Migration does not drop old M2M through-table when ManyToManyField target model changes
When changing the target model of a ManyToManyField, the migration autodetector generates only an AlterField operation. Django does not create a RemoveField + AddField sequence, and the old automatically generated through-table is left in place.
This results in the database schema no longer matching the model definitions. In my case, the stale through-table caused inconsistent M2M behaviour and errors when interacting with the relationship. No data records were lost, but the schema was incorrect until manually fixed.
This behaviour appears inconsistent with how Django handles other destructive schema changes.
Change History
(15)
| Component: |
Database layer (models, ORM) → Migrations
|
| Triage Stage: |
Unreviewed → Accepted
|
| Type: |
Uncategorized → Bug
|
| Owner: |
set to Johanan Oppong Amoateng
|
| Status: |
new → assigned
|
| Patch needs improvement: |
set
|
| Patch needs improvement: |
unset
|
| Triage Stage: |
Accepted → Ready for checkin
|
| Triage Stage: |
Ready for checkin → Accepted
|
| Patch needs improvement: |
set
|
| Patch needs improvement: |
unset
|
| Patch needs improvement: |
set
|
| Has patch: |
set
|
| Patch needs improvement: |
unset
|
| Triage Stage: |
Accepted → Ready for checkin
|
| Resolution: |
→ fixed
|
| Status: |
assigned → closed
|
Thanks, replicated. Patch looks straightforward, would you be willing to submit a PR and extrapolate from similar existing test cases?
django/db/migrations/autodetector.py