Fixed #34333 -- Fixed migration operations ordering when adding index/constraint on new foreign key.

Thanks Simon Charette and David Wobrock for reviews.
This commit is contained in:
Durval Carvalho 2023-03-07 17:00:28 -03:00 committed by Mariusz Felisiak
parent dde2537fbb
commit 4b1bfea284
2 changed files with 79 additions and 0 deletions

View file

@ -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(