django.db.migrations.exceptions.CircularDependencyError [英] django.db.migrations.exceptions.CircularDependencyError
问题描述
我在空数据库上进行Django迁移时遇到问题.当我要迁移时,我有一个循环依赖项错误.与外键相关的两个应用之间的循环依赖错误
I have a problem with Django migrations on empty DB. When I want to migrate I have a circular dependency error. Circular dependency error between two apps that related by foreign keys
/firstapp/models.py
/firstapp/models.py
class Person(models.Model):
...
class Doctor(Person):
hospital = models.ForeignKey('hospital.Hospital', on_delete=models.SET_NULL, null=True, default=None,blank = True)
...
class Patient(Person):
doctor = models.ForeignKey('Doctor', on_delete=models.SET_NULL, null=True, default=None)
/secondapp/models.py
/secondapp/models.py
class Hospital(models.Model):
...
main_doctor = models.ForeignKey('authoriz.Doctor', on_delete=models.SET_NULL, null=True,verbose_name="Main Doctor")
calendar = models.ForeignKey('schedule.Calendar',verbose_name="calendar",null = True)
...
class Seat(models.Model):
hospital = models.ForeignKey('Hospital', on_delete=models.CASCADE)
...
之后
python manage.py migrate
追踪
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/user/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/user/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/project/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/project/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/home/user/project/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 136, in handle
plan = executor.migration_plan(targets)
File "/home/user/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 60, in migration_plan
for migration in self.loader.graph.forwards_plan(target):
File "/home/user/project/lib/python3.5/site-packages/django/db/migrations/graph.py", line 280, in forwards_plan
self.ensure_not_cyclic(target, lambda x: (parent.key for parent in self.node_map[x].parents))
File "/home/user/project/lib/python3.5/site-packages/django/db/migrations/graph.py", line 370, in ensure_not_cyclic
raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.exceptions.CircularDependencyError: authoriz.0001_initial, hospital.0001_initial
感谢您的帮助.
推荐答案
临时注释掉外键以打破循环依赖.看来您可以通过注释掉 Hospital.doctor
来做到这一点.删除现有的迁移,然后运行 makemigrations
重新创建它们.
Temporarily comment out foreign keys to break the circular dependency. It looks like you could do this by commenting out Hospital.doctor
. Remove the existing migrations and run makemigrations
to recreate them.
最后,取消注释外键,然后再次运行 makemigrations
.您应该在没有任何循环依赖的情况下进行迁移.
Finally, uncomment the foreign keys, and run makemigrations
again. You should end up with migrations without any circular dependencies.
这篇关于django.db.migrations.exceptions.CircularDependencyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!