summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2014-07-26 09:21:53 -0700
committerAndrew Godwin <andrew@aeracode.org>2014-07-26 09:22:31 -0700
commit7e708a2536d4828d06f7aa4002fbd5cfc20bef16 (patch)
treef4360d7e2cee7493816b7947fbeb8ca2d7093171
parent3a2badcbb73c2aed6451d2451da1c4fc8d934ab2 (diff)
[1.7.x] Fixed #22944: Bad dependency on FK alteration in autodetector
-rw-r--r--django/db/migrations/autodetector.py15
-rw-r--r--tests/migrations/test_autodetector.py19
2 files changed, 33 insertions, 1 deletions
diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py
index fff5daf08e..7f52b9d07c 100644
--- a/django/db/migrations/autodetector.py
+++ b/django/db/migrations/autodetector.py
@@ -342,6 +342,13 @@ class MigrationAutodetector(object):
isinstance(operation, operations.DeleteModel) and
operation.name.lower() == dependency[1].lower()
)
+ # Field being altered
+ elif dependency[2] is not None and dependency[3] == "alter":
+ return (
+ isinstance(operation, operations.AlterField) and
+ operation.model_name.lower() == dependency[1].lower() and
+ operation.name.lower() == dependency[2].lower()
+ )
# order_with_respect_to being unset for a field
elif dependency[2] is not None and dependency[3] == "order_wrt_unset":
return (
@@ -631,7 +638,7 @@ class MigrationAutodetector(object):
)
)
# Finally, remove the model.
- # This depends on both the removal of all incoming fields
+ # This depends on both the removal/alteration of all incoming fields
# and the removal of all its own related fields, and if it's
# a through model the field that references it.
dependencies = []
@@ -642,6 +649,12 @@ class MigrationAutodetector(object):
related_object.field.name,
False,
))
+ dependencies.append((
+ related_object.model._meta.app_label,
+ related_object.model._meta.object_name,
+ related_object.field.name,
+ "alter",
+ ))
for related_object in model._meta.get_all_related_many_to_many_objects():
dependencies.append((
related_object.model._meta.app_label,
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index 3de9e5d6ba..aca7b52a2b 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -52,6 +52,7 @@ class AutodetectorTests(TestCase):
contract = ModelState("testapp", "Contract", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("publisher", models.ForeignKey("testapp.Publisher"))])
publisher = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=100))])
publisher_with_author = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("name", models.CharField(max_length=100))])
+ publisher_with_aardvark_author = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Aardvark")), ("name", models.CharField(max_length=100))])
publisher_with_book = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("otherapp.Book")), ("name", models.CharField(max_length=100))])
other_pony = ModelState("otherapp", "Pony", [("id", models.AutoField(primary_key=True))])
other_stable = ModelState("otherapp", "Stable", [("id", models.AutoField(primary_key=True))])
@@ -72,6 +73,7 @@ class AutodetectorTests(TestCase):
custom_user = ModelState("thirdapp", "CustomUser", [("id", models.AutoField(primary_key=True)), ("username", models.CharField(max_length=255))], bases=(AbstractBaseUser, ))
custom_user_no_inherit = ModelState("thirdapp", "CustomUser", [("id", models.AutoField(primary_key=True)), ("username", models.CharField(max_length=255))])
aardvark = ModelState("thirdapp", "Aardvark", [("id", models.AutoField(primary_key=True))])
+ aardvark_testapp = ModelState("testapp", "Aardvark", [("id", models.AutoField(primary_key=True))])
aardvark_based_on_author = ModelState("testapp", "Aardvark", [], bases=("testapp.Author", ))
aardvark_pk_fk_author = ModelState("testapp", "Aardvark", [("id", models.OneToOneField("testapp.Author", primary_key=True))])
knight = ModelState("eggs", "Knight", [("id", models.AutoField(primary_key=True))])
@@ -1074,3 +1076,20 @@ class AutodetectorTests(TestCase):
self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="Book")
# Right dependencies?
self.assertEqual(changes['otherapp'][0].dependencies, [("migrations", "0002_second")])
+
+ def test_alter_fk_before_model_deletion(self):
+ """
+ Tests that ForeignKeys are altered _before_ the model they used to
+ refer to are deleted.
+ """
+ # Make state
+ before = self.make_project_state([self.author_name, self.publisher_with_author])
+ after = self.make_project_state([self.aardvark_testapp, self.publisher_with_aardvark_author])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector._detect_changes()
+ # Right number of migrations?
+ self.assertNumberMigrations(changes, 'testapp', 1)
+ self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "AlterField", "DeleteModel"])
+ self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Aardvark")
+ self.assertOperationAttributes(changes, 'testapp', 0, 1, name="author")
+ self.assertOperationAttributes(changes, 'testapp', 0, 2, name="Author")