django.db.migrations.exceptions.CircularDependencyError

问题描述 投票:0回答:4

我在空数据库上进行 Django 迁移时遇到问题。当我想迁移时,出现循环依赖错误。通过外键关联的两个应用程序之间的循环依赖错误

/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

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

感谢您的帮助。

python django django-migrations django-database
4个回答
28
投票

暂时注释掉外键以打破循环依赖。看来你可以通过注释掉

Hospital.doctor
来做到这一点。删除现有的迁移并运行
makemigrations
重新创建它们。

最后,取消注释外键,然后再次运行

makemigrations
。您最终应该得到没有任何循环依赖的迁移。


0
投票

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

class Person(models.Model):
    ...

    class Meta:
        abstract = True

0
投票

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

new_field = models.ForeignKey(ForeignModel, ...)

而是这样做:

new_field = models.ForeignKey("ForeignModel", ...)

0
投票
  1. 删除现有的虚拟环境并创建一个新的。
  2. 在模型文件中,注释掉所有
    ForeignKey
    字段。
  3. 运行迁移并迁移
  4. 取消注释您评论过的
    ForeignKey
    字段
  5. 再次运行迁移并迁移
© www.soinside.com 2019 - 2024. All rights reserved.