From c7ec3c07e78e288a48e87b979dbd12e1fa44fe66 Mon Sep 17 00:00:00 2001 From: Markus Holtermann Date: Sat, 28 Mar 2015 18:57:51 +0100 Subject: [PATCH] Fixed #24537 -- Ignored field order in RenameModel detection Thanks to David Sanders for the report and test and Simon Charette for the review. --- django/db/migrations/autodetector.py | 2 +- tests/migrations/test_autodetector.py | 31 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 786de794df5..a73985d7244 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -76,7 +76,7 @@ class MigrationAutodetector(object): change during renames) """ fields_def = [] - for name, field in fields: + for name, field in sorted(fields): deconstruction = self.deep_deconstruct(field) if field.remote_field and field.remote_field.model: del deconstruction[2]['to'] diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 59654f95393..269d41fd2cd 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -657,6 +657,37 @@ class AutodetectorTests(TestCase): self.assertOperationTypes(changes, 'otherapp', 0, ["RenameField"]) self.assertOperationAttributes(changes, 'otherapp', 0, 0, old_name="author", new_name="writer") + def test_rename_model_with_fks_in_different_position(self): + """ + #24537 - Tests that the order of fields in a model does not influence + the RenameModel detection. + """ + before = self.make_project_state([ + ModelState("testapp", "EntityA", [ + ("id", models.AutoField(primary_key=True)), + ]), + ModelState("testapp", "EntityB", [ + ("id", models.AutoField(primary_key=True)), + ("some_label", models.CharField(max_length=255)), + ("entity_a", models.ForeignKey("testapp.EntityA")), + ]), + ]) + after = self.make_project_state([ + ModelState("testapp", "EntityA", [ + ("id", models.AutoField(primary_key=True)), + ]), + ModelState("testapp", "RenamedEntityB", [ + ("id", models.AutoField(primary_key=True)), + ("entity_a", models.ForeignKey("testapp.EntityA")), + ("some_label", models.CharField(max_length=255)), + ]), + ]) + autodetector = MigrationAutodetector(before, after, MigrationQuestioner({"ask_rename_model": True})) + changes = autodetector._detect_changes() + self.assertNumberMigrations(changes, "testapp", 1) + self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"]) + self.assertOperationAttributes(changes, "testapp", 0, 0, old_name="EntityB", new_name="RenamedEntityB") + def test_fk_dependency(self): """Tests that having a ForeignKey automatically adds a dependency.""" # Make state