From 4b1bfea2846f66f504265cec46ee1fe94ee9c98b Mon Sep 17 00:00:00 2001 From: Durval Carvalho Date: Tue, 7 Mar 2023 17:00:28 -0300 Subject: Fixed #34333 -- Fixed migration operations ordering when adding index/constraint on new foreign key. Thanks Simon Charette and David Wobrock for reviews. --- tests/migrations/test_autodetector.py | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'tests/migrations') diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 6422c82214..44923a3c2e 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -2720,6 +2720,65 @@ class AutodetectorTests(BaseAutodetectorTests): changes, "testapp", 0, 0, model_name="author", constraint=added_constraint ) + def test_add_constraints_with_new_model(self): + book_with_unique_title_and_pony = ModelState( + "otherapp", + "Book", + [ + ("id", models.AutoField(primary_key=True)), + ("title", models.CharField(max_length=200)), + ("pony", models.ForeignKey("otherapp.Pony", models.CASCADE)), + ], + { + "constraints": [ + models.UniqueConstraint( + fields=["title", "pony"], + name="unique_title_pony", + ) + ] + }, + ) + changes = self.get_changes( + [self.book_with_no_author], + [book_with_unique_title_and_pony, self.other_pony], + ) + + self.assertNumberMigrations(changes, "otherapp", 1) + self.assertOperationTypes( + changes, + "otherapp", + 0, + ["CreateModel", "AddField", "AddConstraint"], + ) + + def test_add_index_with_new_model(self): + book_with_index_title_and_pony = ModelState( + "otherapp", + "Book", + [ + ("id", models.AutoField(primary_key=True)), + ("title", models.CharField(max_length=200)), + ("pony", models.ForeignKey("otherapp.Pony", models.CASCADE)), + ], + { + "indexes": [ + models.Index(fields=["title", "pony"], name="index_title_pony"), + ] + }, + ) + changes = self.get_changes( + [self.book_with_no_author], + [book_with_index_title_and_pony, self.other_pony], + ) + + self.assertNumberMigrations(changes, "otherapp", 1) + self.assertOperationTypes( + changes, + "otherapp", + 0, + ["CreateModel", "AddField", "AddIndex"], + ) + def test_remove_constraints(self): """Test change detection of removed constraints.""" changes = self.get_changes( -- cgit v1.2.1