#36791: Migration does not drop old M2M through-table when ManyToManyField target
model changes
-------------------------------------+-------------------------------------
Reporter: Johanan Oppong | Owner: (none)
Amoateng |
Type: Bug | Status: new
Component: Migrations | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):
* component: Database layer (models, ORM) => Migrations
* stage: Unreviewed => Accepted
* type: Uncategorized => Bug
Comment:
Thanks, replicated. Patch looks straightforward, would you be willing to
submit a PR and extrapolate from similar existing test cases?
{{{#!diff
diff --git a/django/db/migrations/autodetector.py
b/django/db/migrations/autodetector.py
index 7cc22c0637..fca73a675d 100644
--- a/django/db/migrations/autodetector.py
+++ b/django/db/migrations/autodetector.py
@@ -1339,7 +1339,8 @@ class MigrationAutodetector:
if old_field_dec != new_field_dec and old_field_name ==
field_name:
both_m2m = old_field.many_to_many and
new_field.many_to_many
neither_m2m = not old_field.many_to_many and not
new_field.many_to_many
- if both_m2m or neither_m2m:
+ target_changed = both_m2m and new_field_dec[2]["to"] !=
old_field_dec[2]["to"]
+ if (both_m2m or neither_m2m) and not target_changed:
# Either both fields are m2m or neither is
preserve_default = True
if (
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/36791#comment:1>