From 00e3b9a2a992ee0b7288eeeb03e7cbd52ebc6dce Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Tue, 8 Apr 2014 12:30:25 +0700 Subject: Fixed #22397 -- Issues removing M2M field with explicit through model. Changed the migration autodetector to remove models last so that FK and M2M fields will not be left as dangling references. Added a check in the migration state renderer to error out in the presence of dangling references instead of leaving them as strings. Fixed a bug in the sqlite backend to handle the deletion of M2M fields with "through" models properly (i.e., do nothing successfully). Thanks to melinath for report, loic for tests and andrewgodwin and charettes for assistance with architecture. --- django/db/backends/sqlite3/schema.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'django/db/backends/sqlite3') diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 216ff0958a..a8417d4f66 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -121,11 +121,15 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): Removes a field from a model. Usually involves deleting a column, but for M2Ms may involve deleting a table. """ - # Special-case implicit M2M tables - if isinstance(field, ManyToManyField) and field.rel.through._meta.auto_created: - return self.delete_model(field.rel.through) + # M2M fields are a special case + if isinstance(field, ManyToManyField): + # For implicit M2M tables, delete the auto-created table + if field.rel.through._meta.auto_created: + self.delete_model(field.rel.through) + # For explicit "through" M2M fields, do nothing # For everything else, remake. - self._remake_table(model, delete_fields=[field]) + else: + self._remake_table(model, delete_fields=[field]) def alter_field(self, model, old_field, new_field, strict=False): """ -- cgit v1.3