diff options
| author | Sergey Fedoseev <fedoseev.sergey@gmail.com> | 2014-09-17 11:34:10 +0600 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-09-23 10:03:37 -0400 |
| commit | 463952d94014ac2ea70a96828ddbf1330b504fc7 (patch) | |
| tree | 52716d2a177c77419754fe14ed3d221a46886164 | |
| parent | 83cd18633fb13924d8a50a1cd4ed21cb9552a635 (diff) | |
Fixed #23503 -- Fixed renaming of model with self-referential m2m field.
| -rw-r--r-- | django/db/backends/schema.py | 6 | ||||
| -rw-r--r-- | docs/releases/1.7.1.txt | 3 | ||||
| -rw-r--r-- | tests/migrations/test_operations.py | 16 |
3 files changed, 25 insertions, 0 deletions
diff --git a/django/db/backends/schema.py b/django/db/backends/schema.py index fadb34eac7..8bc16dab2d 100644 --- a/django/db/backends/schema.py +++ b/django/db/backends/schema.py @@ -724,6 +724,12 @@ class BaseDatabaseSchemaEditor(object): old_field.rel.through._meta.get_field_by_name(old_field.m2m_reverse_field_name())[0], new_field.rel.through._meta.get_field_by_name(new_field.m2m_reverse_field_name())[0], ) + self.alter_field( + new_field.rel.through, + # for self-referential models we need to alter field from the other end too + old_field.rel.through._meta.get_field_by_name(old_field.m2m_field_name())[0], + new_field.rel.through._meta.get_field_by_name(new_field.m2m_field_name())[0], + ) def _create_index_name(self, model, column_names, suffix=""): """ diff --git a/docs/releases/1.7.1.txt b/docs/releases/1.7.1.txt index ff8228103d..aa7d7f90a0 100644 --- a/docs/releases/1.7.1.txt +++ b/docs/releases/1.7.1.txt @@ -58,3 +58,6 @@ Bugfixes * Added quoting of constraint names in the SQL generated by migrations to prevent crash with uppercase characters in the name (:ticket:`23065`). + +* Fixed renaming of models with a self-referential many-to-many field + (``ManyToManyField('self')``) (:ticket:`23503`). diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 82292ac69f..bddb09f9e7 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -472,6 +472,22 @@ class OperationTests(OperationTestBase): self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id")) self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id")) + def test_rename_model_with_self_referential_m2m(self): + app_label = "test_rename_model_with_self_referential_m2m" + + project_state = self.apply_operations(app_label, ProjectState(), operations=[ + migrations.CreateModel("ReflexivePony", fields=[ + ("ponies", models.ManyToManyField("self")), + ]), + ]) + project_state = self.apply_operations(app_label, project_state, operations=[ + migrations.RenameModel("ReflexivePony", "ReflexivePony2"), + ]) + apps = project_state.render() + Pony = apps.get_model(app_label, "ReflexivePony2") + pony = Pony.objects.create() + pony.ponies.add(pony) + def test_add_field(self): """ Tests the AddField operation. |
