簡體   English   中英

Django 1.8循環依賴錯誤

[英]Django 1.8 Circular Dependecy error

我在網上找不到解決此問題的方法。 我所擁有的就是"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." 從這里: 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

對於那些遇到了CircularDependencyError的用戶-不一定需要使用ForeignKey-最好去循環一下

python manage.py makemigrations app_name; python manage.py遷移

對於項目中的每個應用程序,一一對應。

這適用於Django 1.10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM