From 32e82c27231b114effe265bd5e1b78a60d131d4b Mon Sep 17 00:00:00 2001
From: Nils Liberg <nils@nilsliberg.se>
Date: Mon, 24 Mar 2014 16:36:17 +0100
Subject: [PATCH] Fix incorrect CircularDependencyError
In the build_graph method of the MigrationLoader class the self.graph.root_nodes(...) method was run on a dependency graph that was not yet fully constructed. This could cause the current node (key) to be incorrectly included among the returned root nodes and since the parent is drawn from this set one could get circular dependencies not present in the real dependency graph.
Perhaps this change will fix the problems described in Django ticket #22325 and #22277.
---
django/db/migrations/loader.py | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/django/db/migrations/loader.py b/django/db/migrations/loader.py
index ee4f4c9..316ff95 100644
a
|
b
|
def build_graph(self, ignore_unmigrated=False):
|
194 | 194 | self.graph.add_node(key, migration) |
195 | 195 | for key, migration in normal.items(): |
196 | 196 | for parent in migration.dependencies: |
| 197 | if parent is not None and parent[1] != "__first__": |
| 198 | self.graph.add_dependency(key, parent) |
| 199 | for key, migration in normal.items(): |
| 200 | for parent in migration.dependencies: |
197 | 201 | # Special-case __first__, which means "the first migration" for |
198 | 202 | # migrated apps, and is ignored for unmigrated apps. It allows |
199 | 203 | # makemigrations to declare dependencies on apps before they |
… |
… |
def build_graph(self, ignore_unmigrated=False):
|
229 | 233 | parent = list(self.graph.root_nodes(parent[0]))[0] |
230 | 234 | else: |
231 | 235 | raise ValueError("Dependency on unknown app %s" % parent[0]) |
232 | | if parent is not None: |
233 | | self.graph.add_dependency(key, parent) |
| 236 | if parent is not None: |
| 237 | self.graph.add_dependency(key, parent) |
234 | 238 | |
235 | 239 | def detect_conflicts(self): |
236 | 240 | """ |