Ale*_*lla 0 python django django-migrations
我无法在线找到这个问题的解决方案.我所拥有的只是"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
Run Code Online (Sandbox Code Playgroud)
我不知道如何找到CircularDependency的位置,我不知道如何解决它.正如您所看到的,迁移是n001 - 这是因为我尝试删除它们并再次执行它,但是没有用.请帮忙.
您应该创建一个没有外键的迁移,然后再添加FK.
让我们假设您要创建这些模型:
libros/models.py:
class Libro(models.Model):
name = models.CharField(max_length=20)
perfile = models.ForeignKey('perfiles.Perfile', null=True)
Run Code Online (Sandbox Code Playgroud)
perfiles/models.py:
class Perfile(models.Model):
name = models.CharField(max_length=20)
libro = models.ForeignKey('libros.Libro', null=True)
Run Code Online (Sandbox Code Playgroud)
当然,由于循环依赖,你不能这样做.因此,在Libro模型中注释掉外键:
class Libro(models.Model):
name = models.CharField(max_length=20)
# perfile = models.ForeignKey('perfiles.Perfile', null=True)
Run Code Online (Sandbox Code Playgroud)
并运行两次迁移:
python manage.py makemigrations libros
python manage.py makemigrations perfiles
Run Code Online (Sandbox Code Playgroud)
之后取消注释模型中的perfile外键Libro并运行另一个迁移:
python manage.py makemigrations libros
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2359 次 |
| 最近记录: |