gpt4 book ai didi

python - Django 1.8 循环依赖错误

转载 作者:行者123 更新时间:2023-12-01 22:29:32 26 4
gpt4 key购买 nike

我无法在线找到此问题的解决方案。我所拥有的只是 “要手动解决 CircularDependencyError,将循环依赖循环中的一个 ForeignKeys 分解为单独的迁移,并将对另一个应用程序的依赖与它一起移动。如果您不确定,请参阅如何 makemigrations当被要求从您的模型创建全新的迁移时处理问题。在 Django 的 future 版本中,squashmigrations 将被更新以尝试自行解决这些错误。“ 从这里:docs .我是 django 迁移的新手,我想要一个更易于理解和易于理解的答案。

我收到这个错误:

raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.graph.CircularDependencyError: libros.0001_initial, perfiles.0001_initial

我不知道如何找到 CircularDependency 在哪里,我不确定如何解决它。如您所见,迁移是 n001 - 那是因为我尝试删除它们并再次执行,但没有成功。请帮忙。

最佳答案

您应该创建一个没有外键的迁移,然后再添加 FK。

假设您要创建这些模型:

libros/models.py:

class Libro(models.Model):
name = models.CharField(max_length=20)
perfile = models.ForeignKey('perfiles.Perfile', null=True)

perfiles/models.py:

class Perfile(models.Model):
name = models.CharField(max_length=20)
libro = models.ForeignKey('libros.Libro', null=True)

当然你不能这样做,因为循环依赖。所以在 Libro 模型中注释掉外键:

class Libro(models.Model):
name = models.CharField(max_length=20)
# perfile = models.ForeignKey('perfiles.Perfile', null=True)

并运行两个迁移:

python manage.py makemigrations libros
python manage.py makemigrations perfiles

之后取消注释 Libro 模型中的 perfile 外键并运行另一个迁移:

python manage.py makemigrations libros

关于python - Django 1.8 循环依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30204409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com