Opened 74 minutes ago
#36791 new Uncategorized
Migration does not drop old M2M through-table when ManyToManyField target model changes
| Reported by: | Johanan Oppong Amoateng | Owned by: | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Johanan Oppong Amoateng | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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.