Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #21114 -- Migrations must not have a dependency to themselves.

  • Loading branch information...
commit 5a424c2393bf9f2524b8075723e90298c1297846 1 parent 73a610d
Markus Holtermann authored September 17, 2013 timgraham committed September 17, 2013
6  django/db/migrations/autodetector.py
@@ -106,7 +106,8 @@ def _detect_changes(self):
106 106
                     )
107 107
                 )
108 108
                 for field_name, other_app_label, other_model_name in related_fields:
109  
-                    self.add_dependency(app_label, other_app_label)
  109
+                    if app_label != other_app_label:
  110
+                        self.add_dependency(app_label, other_app_label)
110 111
                 del pending_add[app_label, model_name]
111 112
             # Ah well, we'll need to split one. Pick deterministically.
112 113
             else:
@@ -140,7 +141,8 @@ def _detect_changes(self):
140 141
                 ),
141 142
                 new = True,
142 143
             )
143  
-            self.add_dependency(app_label, other_app_label)
  144
+            if app_label != other_app_label:
  145
+                self.add_dependency(app_label, other_app_label)
144 146
         # Removing models
145 147
         removed_models = set(old_model_keys) - set(new_model_keys)
146 148
         for app_label, model_name in removed_models:
55  tests/migrations/test_autodetector.py
@@ -16,8 +16,11 @@ class AutodetectorTests(TestCase):
16 16
     author_name_longer = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=400))])
17 17
     author_name_renamed = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("names", models.CharField(max_length=200))])
18 18
     author_with_book = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
  19
+    author_with_publisher = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("publisher", models.ForeignKey("testapp.Publisher"))])
19 20
     author_proxy = ModelState("testapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author", ))
20 21
     author_proxy_notproxy = ModelState("testapp", "AuthorProxy", [], {}, ("testapp.author", ))
  22
+    publisher = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=100))])
  23
+    publisher_with_author = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("name", models.CharField(max_length=100))])
21 24
     other_pony = ModelState("otherapp", "Pony", [("id", models.AutoField(primary_key=True))])
22 25
     other_stable = ModelState("otherapp", "Stable", [("id", models.AutoField(primary_key=True))])
23 26
     third_thing = ModelState("thirdapp", "Thing", [("id", models.AutoField(primary_key=True))])
@@ -205,6 +208,29 @@ def test_fk_dependency(self):
205 208
         self.assertEqual(migration2.dependencies, [("testapp", "auto_1")])
206 209
         self.assertEqual(migration3.dependencies, [("otherapp", "auto_1")])
207 210
 
  211
+    def test_same_app_no_fk_dependency(self):
  212
+        """
  213
+        Tests that a migration with a FK between two models of the same app
  214
+        does not have a dependency to itself.
  215
+        """
  216
+        # Make state
  217
+        before = self.make_project_state([])
  218
+        after = self.make_project_state([self.author_with_publisher, self.publisher])
  219
+        autodetector = MigrationAutodetector(before, after)
  220
+        changes = autodetector._detect_changes()
  221
+        # Right number of migrations?
  222
+        self.assertEqual(len(changes['testapp']), 1)
  223
+        # Right number of actions?
  224
+        migration = changes['testapp'][0]
  225
+        self.assertEqual(len(migration.operations), 2)
  226
+        # Right actions?
  227
+        action = migration.operations[0]
  228
+        self.assertEqual(action.__class__.__name__, "CreateModel")
  229
+        action = migration.operations[1]
  230
+        self.assertEqual(action.__class__.__name__, "CreateModel")
  231
+        # Right dependencies?
  232
+        self.assertEqual(migration.dependencies, [])
  233
+
208 234
     def test_circular_fk_dependency(self):
209 235
         """
210 236
         Tests that having a circular ForeignKey dependency automatically
@@ -239,6 +265,35 @@ def test_circular_fk_dependency(self):
239 265
         self.assertEqual(migration2.dependencies, [])
240 266
         self.assertEqual(set(migration3.dependencies), set([("otherapp", "auto_1"), ("testapp", "auto_1")]))
241 267
 
  268
+    def test_same_app_circular_fk_dependency(self):
  269
+        """
  270
+        Tests that a migration with a FK between two models of the same app
  271
+        does not have a dependency to itself.
  272
+        """
  273
+        # Make state
  274
+        before = self.make_project_state([])
  275
+        after = self.make_project_state([self.author_with_publisher, self.publisher_with_author])
  276
+        autodetector = MigrationAutodetector(before, after)
  277
+        changes = autodetector._detect_changes()
  278
+        # Right number of migrations?
  279
+        self.assertEqual(len(changes['testapp']), 2)
  280
+        # Right number of actions?
  281
+        migration1 = changes['testapp'][0]
  282
+        self.assertEqual(len(migration1.operations), 2)
  283
+        migration2 = changes['testapp'][1]
  284
+        self.assertEqual(len(migration2.operations), 1)
  285
+        # Right actions?
  286
+        action = migration1.operations[0]
  287
+        self.assertEqual(action.__class__.__name__, "CreateModel")
  288
+        action = migration1.operations[1]
  289
+        self.assertEqual(action.__class__.__name__, "CreateModel")
  290
+        action = migration2.operations[0]
  291
+        self.assertEqual(action.__class__.__name__, "AddField")
  292
+        self.assertEqual(action.name, "publisher")
  293
+        # Right dependencies?
  294
+        self.assertEqual(migration1.dependencies, [])
  295
+        self.assertEqual(migration2.dependencies, [("testapp", "auto_1")])
  296
+
242 297
     def test_unique_together(self):
243 298
         "Tests unique_together detection"
244 299
         # Make state

0 notes on commit 5a424c2

Please sign in to comment.
Something went wrong with that request. Please try again.