gpt4 book ai didi

python - 重新排序初始迁移操作

转载 作者:行者123 更新时间:2023-12-03 20:29:30 25 4
gpt4 key购买 nike

每次我尝试 migrate 我的初始迁移时,在 makemigrations 之后,我都会收到如下错误:

django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'Project.Class'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)

我认为发生这种情况的原因是 0001_initial.py 迁移中模型 operations 的顺序不正确。从其他类继承的类的操作被添加到它们的父类之前。在我重新排序操作后,它起作用了:Process finished with exit code 0。凉爽的!但是,如何让 makemigrations 工作而不每次都这样做呢?

谢谢!

附言。我尝试在模型的 __init__.py 中重新排序模型的导入顺序,但没有成功。

最佳答案

如果您的 多个应用 在您的 Django 项目 和一个应用模型的模型中引用了另一个应用程序的 - 这可能会导致此类冲突。

建议分别为每个应用创建迁移,并在引用迁移文件时将另一个应用迁移作为依赖项引用。

python manage.py makemigrations app-one
python manage.py makemigrations app-two

# example of referencing dependent migration,
# so app-two 0001 migrations runs after app-one 0001 migration
# app-two/migrations/0001-initial.py

dependencies = [("app-one", "0001-init.py")]

关于python - 重新排序初始迁移操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59221337/

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