diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 2b8759b29a..d8b2cae3fa 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -169,6 +169,7 @@ class MigrationAutodetector(object): kept_models = set(old_model_keys).intersection(new_model_keys) old_fields = set() new_fields = set() + unique_together_operations = [] for app_label, model_name in kept_models: old_model_state = self.from_state.models[app_label, model_name] new_model_state = self.to_state.models[app_label, model_name] @@ -176,15 +177,16 @@ class MigrationAutodetector(object): # always come before AlterFields even on separate models) old_fields.update((app_label, model_name, x) for x, y in old_model_state.fields) new_fields.update((app_label, model_name, x) for x, y in new_model_state.fields) - # Unique_together changes + # Unique_together changes. Operations will be added to migration a + # bit later, after fields creation. See ticket #22035. if old_model_state.options.get("unique_together", set()) != new_model_state.options.get("unique_together", set()): - self.add_to_migration( + unique_together_operations.append(( app_label, operations.AlterUniqueTogether( name=model_name, unique_together=new_model_state.options.get("unique_together", set()), ) - ) + )) # New fields for app_label, model_name, field_name in new_fields - old_fields: old_model_state = self.from_state.models[app_label, model_name] @@ -263,6 +265,8 @@ class MigrationAutodetector(object): field=new_model_state.get_field_by_name(field_name), ) ) + for app_label, operation in unique_together_operations: + self.add_to_migration(app_label, operation) # Alright, now add internal dependencies for app_label, migrations in self.migrations.items(): for m1, m2 in zip(migrations, migrations[1:]): diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index dca971dd8e..590858a117 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -29,6 +29,7 @@ class AutodetectorTests(TestCase): book = ModelState("otherapp", "Book", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("title", models.CharField(max_length=200))]) book_unique = ModelState("otherapp", "Book", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("title", models.CharField(max_length=200))], {"unique_together": [("author", "title")]}) book_unique_2 = ModelState("otherapp", "Book", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("title", models.CharField(max_length=200))], {"unique_together": [("title", "author")]}) + book_unique_3 = ModelState("otherapp", "Book", [("id", models.AutoField(primary_key=True)), ("newfield", models.IntegerField()), ("author", models.ForeignKey("testapp.Author")), ("title", models.CharField(max_length=200))], {"unique_together": [("title", "newfield")]}) edition = ModelState("thirdapp", "Edition", [("id", models.AutoField(primary_key=True)), ("book", models.ForeignKey("otherapp.Book"))]) custom_user = ModelState("thirdapp", "CustomUser", [("id", models.AutoField(primary_key=True)), ("username", models.CharField(max_length=255))]) @@ -333,6 +334,24 @@ class AutodetectorTests(TestCase): self.assertEqual(action.name, "book") self.assertEqual(action.unique_together, set([("title", "author")])) + def test_add_field_and_unique_together(self): + "Tests that added fields will be created before using them in unique together" + before = self.make_project_state([self.author_empty, self.book]) + after = self.make_project_state([self.author_empty, self.book_unique_3]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + # Right number of migrations? + self.assertEqual(len(changes['otherapp']), 1) + # Right number of actions? + migration = changes['otherapp'][0] + self.assertEqual(len(migration.operations), 2) + # Right actions order? + action1 = migration.operations[0] + action2 = migration.operations[1] + self.assertEqual(action1.__class__.__name__, "AddField") + self.assertEqual(action2.__class__.__name__, "AlterUniqueTogether") + self.assertEqual(action2.unique_together, set([("title", "newfield")])) + def test_proxy_ignorance(self): "Tests that the autodetector correctly ignores proxy models" # First, we test adding a proxy model