首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django.db.migrations.exceptions.CircularDependencyError

django.db.migrations.exceptions.CircularDependencyError
EN

Stack Overflow用户
提问于 2016-11-20 22:30:39
回答 3查看 9.3K关注 0票数 11

我对空DB上的Django迁移有问题。当我想要迁移时,我有一个循环依赖错误。外键关联的两个app出现循环依赖错误

/firstapp/models.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
    ...

之后

代码语言:javascript
复制
python manage.py migrate

回溯

代码语言:javascript
复制
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

谢谢你的帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-21 01:20:03

暂时注释掉外键以中断循环依赖。看起来你可以通过注释掉Hospital.doctor来做到这一点。删除现有迁移并运行makemigrations重新创建它们。

最后,取消注释外键,并再次运行makemigrations。您应该以不存在任何循环依赖关系的迁移结束。

票数 16
EN

Stack Overflow用户

发布于 2016-11-21 02:01:17

如果你添加你的日历模型,它应该是有用的。但是,在没有抽象模式的情况下,不要使用继承。

代码语言:javascript
复制
class Person(models.Model):
    ...

    class Meta:
        abstract = True
票数 0
EN

Stack Overflow用户

发布于 2021-08-26 07:50:07

就像您可能已经将它们定义为以下内容:

代码语言:javascript
复制
new_field = models.ForeignKey(ForeignModel, ...)

取而代之的是:

代码语言:javascript
复制
new_field = models.ForeignKey("ForeignModel", ...)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40705237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档