Python Django迁移构建图有bug吗?
挤压迁移后运行测试时,我会收到以下错误报告:Python Django迁移构建图有bug吗?,python,django,database-migration,Python,Django,Database Migration,挤压迁移后运行测试时,我会收到以下错误报告: lib/python2.7/site-packages/django/db/migrations/loader.py:220: KeyError 以下是源代码: def build_graph(self): """ Builds a migration dependency graph using both the disk and database. You'll need to rebuild the g
lib/python2.7/site-packages/django/db/migrations/loader.py:220: KeyError
以下是源代码:
def build_graph(self):
"""
Builds a migration dependency graph using both the disk and database.
You'll need to rebuild the graph if you apply migrations. This isn't
usually a problem as generally migration stuff runs in a one-shot process.
"""
# Load disk data
self.load_disk()
# Load database data
if self.connection is None:
self.applied_migrations = set()
else:
recorder = MigrationRecorder(self.connection)
self.applied_migrations = recorder.applied_migrations()
# Do a first pass to separate out replacing and non-replacing migrations
normal = {}
replacing = {}
for key, migration in self.disk_migrations.items():
if migration.replaces:
replacing[key] = migration
else:
normal[key] = migration
# Calculate reverse dependencies - i.e., for each migration, what depends on it?
# This is just for dependency re-pointing when applying replacements,
# so we ignore run_before here.
reverse_dependencies = {}
for key, migration in normal.items():
for parent in migration.dependencies:
reverse_dependencies.setdefault(parent, set()).add(key)
# Carry out replacements if we can - that is, if all replaced migrations
# are either unapplied or missing.
for key, migration in replacing.items():
# Ensure this replacement migration is not in applied_migrations
self.applied_migrations.discard(key)
# Do the check. We can replace if all our replace targets are
# applied, or if all of them are unapplied.
applied_statuses = [(target in self.applied_migrations) for target in migration.replaces]
can_replace = all(applied_statuses) or (not any(applied_statuses))
if not can_replace:
continue
# Alright, time to replace. Step through the replaced migrations
# and remove, repointing dependencies if needs be.
for replaced in migration.replaces:
if replaced in normal:
# We don't care if the replaced migration doesn't exist;
# the usage pattern here is to delete things after a while.
del normal[replaced]
for child_key in reverse_dependencies.get(replaced, set()):
if child_key in migration.replaces:
continue
normal[child_key].dependencies.remove(replaced)
最后一行抛出以下内容:
KeyError: ('my_app', u'0001_initial')
您可以看到问题是正常字典中不存在子项。上一行检查子密钥是否在迁移中。替换,因为在这种情况下,子密钥迁移可能已被删除。但是,子密钥可能正在替换另一个迁移,因此即使它不在migration.replaces中,也可能已被删除
您同意这是django代码中的一个bug吗?这似乎是django 1.7中的一个bug。从bug报告来看,它似乎是Django 1.8的一部分。我们将等到升级Django之后再压缩更多迁移。我也遇到了同样的问题。我正在挤压几个应用程序,突然发现这个错误。我将尝试调试它,假设问题出现在最后一个压缩的应用程序中。