summaryrefslogtreecommitdiff
path: root/tests/migrations
diff options
context:
space:
mode:
authorDavid Wobrock <david.wobrock@gmail.com>2022-06-02 12:10:27 +0200
committerGitHub <noreply@github.com>2022-06-02 12:10:27 +0200
commit798b6c23ee52c675dd0f0e233c50cddd7ff15657 (patch)
treeea54f9ea610f11d4f18f99f8472e06c5e8e5e1c3 /tests/migrations
parent6f73eb9d90cfec684529aab48d517e3d6449ba8c (diff)
downloaddjango-798b6c23ee52c675dd0f0e233c50cddd7ff15657.tar.gz
Fixed #31788 -- Fixed migration optimization after altering field to ManyToManyField.
This makes AddField() used for altering to ManyToManyField, dependent on the prior RemoveField.
Diffstat (limited to 'tests/migrations')
-rw-r--r--tests/migrations/test_autodetector.py50
1 files changed, 46 insertions, 4 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index 547e0b32c5..33411c9e3e 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -868,6 +868,18 @@ class AutodetectorTests(TestCase):
"unique_together": {("title", "newfield2")},
},
)
+ book_unique_together = ModelState(
+ "otherapp",
+ "Book",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
+ ("title", models.CharField(max_length=200)),
+ ],
+ {
+ "unique_together": {("author", "title")},
+ },
+ )
attribution = ModelState(
"otherapp",
"Attribution",
@@ -3798,16 +3810,16 @@ class AutodetectorTests(TestCase):
# Right number/type of migrations?
self.assertNumberMigrations(changes, "testapp", 1)
self.assertOperationTypes(
- changes, "testapp", 0, ["RemoveField", "AddField", "DeleteModel"]
+ changes, "testapp", 0, ["RemoveField", "DeleteModel", "AddField"]
)
self.assertOperationAttributes(
changes, "testapp", 0, 0, name="publishers", model_name="author"
)
+ self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
self.assertOperationAttributes(
- changes, "testapp", 0, 1, name="publishers", model_name="author"
+ changes, "testapp", 0, 2, name="publishers", model_name="author"
)
- self.assertOperationAttributes(changes, "testapp", 0, 2, name="Publisher")
- self.assertOperationFieldAttributes(changes, "testapp", 0, 1, max_length=100)
+ self.assertOperationFieldAttributes(changes, "testapp", 0, 2, max_length=100)
def test_non_circular_foreignkey_dependency_removal(self):
"""
@@ -4346,6 +4358,36 @@ class AutodetectorTests(TestCase):
changes, "testapp", 0, [("otherapp", "__first__")]
)
+ def test_alter_unique_together_fk_to_m2m(self):
+ changes = self.get_changes(
+ [self.author_name, self.book_unique_together],
+ [
+ self.author_name,
+ ModelState(
+ "otherapp",
+ "Book",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ("author", models.ManyToManyField("testapp.Author")),
+ ("title", models.CharField(max_length=200)),
+ ],
+ ),
+ ],
+ )
+ self.assertNumberMigrations(changes, "otherapp", 1)
+ self.assertOperationTypes(
+ changes, "otherapp", 0, ["AlterUniqueTogether", "RemoveField", "AddField"]
+ )
+ self.assertOperationAttributes(
+ changes, "otherapp", 0, 0, name="book", unique_together=set()
+ )
+ self.assertOperationAttributes(
+ changes, "otherapp", 0, 1, model_name="book", name="author"
+ )
+ self.assertOperationAttributes(
+ changes, "otherapp", 0, 2, model_name="book", name="author"
+ )
+
def test_alter_field_to_fk_dependency_other_app(self):
changes = self.get_changes(
[self.author_empty, self.book_with_no_author_fk],