diff options
author | Durval Carvalho <durvalcsouza@outlook.com> | 2023-03-07 17:00:28 -0300 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-03-10 07:05:11 +0100 |
commit | 4b1bfea2846f66f504265cec46ee1fe94ee9c98b (patch) | |
tree | e5eb419c2275266847fcea119d24657524c0b433 /tests/migrations | |
parent | dde2537fbb04ad78a673092a931b449245a2d6ae (diff) | |
download | django-4b1bfea2846f66f504265cec46ee1fe94ee9c98b.tar.gz |
Fixed #34333 -- Fixed migration operations ordering when adding index/constraint on new foreign key.
Thanks Simon Charette and David Wobrock for reviews.
Diffstat (limited to 'tests/migrations')
-rw-r--r-- | tests/migrations/test_autodetector.py | 59 |
1 files changed, 59 insertions, 0 deletions
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( |