Django 1.8循环依赖错误 [英] Django 1.8 Circular Dependecy error

查看:316
本文介绍了Django 1.8循环依赖错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在线查找此问题的解决方案。我所有的都是要手动解析CircularDependencyError,请将循环依赖循环中的一个ForeignKey分解为单独的迁移,并使用它移动其他应用程序的依赖关系如果您不确定,在要求您从模型中创建全新迁移时,请参阅如何处理问题。在未来的Django版本中,squashmigrations将被更新,以尝试解决这些错误。从这里: docs 。我很喜欢django迁移,我喜欢更容易理解和容易的回答。

I'm having trouble finding solutions to this problem online. All I have is "To manually resolve a CircularDependencyError, break out one of the ForeignKeys in the circular dependency loop into a separate migration, and move the dependency on the other app with it. If you’re unsure, see how makemigrations deals with the problem when asked to create brand new migrations from your models. In a future release of Django, squashmigrations will be updated to attempt to resolve these errors itself." from here: docs . I'm kind of new to django migrations, I'd love a more comprehensible and easy to follow answer.

我收到这个错误:

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

我不知道如何找到是循环依赖,我不知道如何解决它。正如你所看到的,迁移是n001 - 这是因为我尝试擦除它们并再次执行,没有工作。请帮助。

I don't know how to find where is the CircularDependency and I'm not sure how to solve it. As you can see, the migrations are n001 - that's because I tried erasing them and doing it again, didn't work. Please help.

推荐答案

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

You should create one migration without foreign key and add the FK later.

假设您要创建这些模型:

Lets suppose that you want to create these models:

libros / models.py

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

perfiles / models.py

perfiles/models.py:

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

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

Of course you can't do it because of the circular dependency. So comment out foreign key in the Libro model:

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

在取消注释后,$ $ c> perfile 外键在 Libro 模型中运行另一个迁移:

After that uncomment the perfile foreign key in the Libro model and run another migration:

python manage.py makemigrations libros

这篇关于Django 1.8循环依赖错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆