Opened 8 years ago

Closed 8 years ago

#25614 closed Bug (duplicate)

Changing ForeignKey(on_delete=...) unnecessarily drops and recreates constraints

Reported by: Jeremy Dunck Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Recently I changed the on_delete attributes of many ForeignKeys on a MySQL database.

This resulted in an auto-detected migration that drops and recreates the constraint for each FK, despite the effective constraint remaining the same (e.g. FOREIGN KEY (modified_by_id) REFERENCES common_user (id); )

The autodetection just compares the deconstruction.

The schema editor drops the FK and recreates it.

It's not entirely clear to me which part needs to be changed - whether the deconstruction should not include on_delete, or whether the autodetector should discard on_delete from consideration, or whether the editor should check for parameters that actually change the effective FK.

What is clear is that dropping and recreating FKs isn't a fast operation on large databases, and it would be good to avoid where possible.

Change History (3)

comment:1 Changed 8 years ago by Jeremy Dunck

Here's the commit that introduced on_delete on ForeignKey.on_delete. Perhaps Andrew recalls the rationale of adding it.

comment:2 Changed 8 years ago by Jeremy Dunck

And now I've tracked it to #23288, but still think adding on_delete was likely unneeded.

comment:3 Changed 8 years ago by Tim Graham

Resolution: duplicate
Status: newclosed
Summary: MigrationAutodetector is too greedy when ForeignKey(on_delete=...) is changedChanging ForeignKey(on_delete=...) unnecessarily drops and recreates constraints

Looks like a duplicate of #25253. We don't currently exclude any model field attributes from deconstruct so that's unlikely to be the solution unless we change the design decision around that.

Note: See TracTickets for help on using tickets.
Back to Top