gpt4 book ai didi

python - South 在不同的应用程序中遵循什么迁移顺序?

转载 作者:太空狗 更新时间:2023-10-29 20:33:54 29 4
gpt4 key购买 nike

我最近开始在我的 Django 项目中使用 South 进行迁移。一切都很顺利,直到最近我遇到了一个特殊的问题。

我的项目中有两个应用程序,例如 App-A 和 App-B。 App-A 中的模型具有指向 App-B 中模型的外键。当我尝试构建我的系统时,我运行了 syndb,它创建了所有 auth_south_ 表。然后我运行 migrate 抛出了错误。当它尝试从引用 App-B 的模型的 App-A 创建模型时,模型 App-B 尚未迁移/创建,因此出现错误。

为了解决这个问题,我不得不先手动迁移 App-B,然后再迁移 App-A。我在这里做错了什么吗? South 应该如何知道跨应用程序的迁移顺序?

谢谢。

最佳答案

这解释了它https://south.readthedocs.io/en/latest/dependencies.html .

Migrations for apps are nice ‘n all, but when you start writing alarge project, with a lot of apps, you realise you have foreign keyrelationships between apps and working out what order migrations wouldneed to be applied in for each app is just painful.

Luckily, we also had this problem, so South has a dependency system.Inside a migration, you can declare that it depends on having anotherapp having run a certain migration first; for example, if my app“forum” depends on the “accounts” app having created its user profiletable, we can do:

# forum/migrations/0002_post.py class Migration:

depends_on = (
("accounts", "0003_add_user_profile"),
)

def forwards(self):

Then, if you try and migrate to or beyond 0002_post in the forum app, it will first make sure accounts is migrated at leastup to 0003_add_user_profile, and if not will migrate it for you.

Dependencies also work in reverse; South knows not to undo that0003_add_user_profile migration until it has undone the 0002_postmigration.

You can have multiple dependencies, and all sorts of wacky structures;there are, however, two rules:

No circular dependencies (two or more migrations depending on eachother) No upwards dependencies in the same app (so you can’t make0002_post in the forum app depend on 0003_room in the same app, eitherdirectly or through a dependency chain.

关于python - South 在不同的应用程序中遵循什么迁移顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7474745/

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