Django南部循环依赖问题

8
我有一个应用程序(我们称其为MyApp)在Django 1.5项目中。 MyApp定义了一个自定义用户模型(MyUser)。该项目使用另一个应用程序(AnotherApp),该应用程序引用MyUser。MyApp引用AnotherApp中的字段。
在我的开发笔记本电脑上一切正常。我正在尝试在服务器上部署我的项目,当我来到迁移步骤时,MyApp由于对AnotherApp的依赖而失败,而AnotherApp由于对MyApp的依赖而失败(我已尝试独立迁移应用程序)。两者都在各自的第一个迁移(0001)上失败。
Running migrations for myapp:
 - Migrating forwards to 0017_auto__blah_blah.
 > myapp:0001_initial
FATAL ERROR - The following SQL query failed: ALTER TABLE "myapp_mymodel_othermodel" ADD CONSTRAINT "othermodel_id_refs_id_ae052c6d" FOREIGN KEY ("othermodel_id") REFERENCES "anotherapp_othermodel" ("id") DEFERRABLE INITIALLY DEFERRED;
The error was: relation "anotherapp_othermodel" does not exist

Error in migration: myapp:0001_initial
DatabaseError: relation "anotherapp_othermodel" does not exist


Running migrations for anotherapp:
 - Migrating forwards to 0008_blah_blah.
 > anotherapp:0001_initial
FATAL ERROR - The following SQL query failed: ALTER TABLE "anotherapp_othermodel" ADD CONSTRAINT "creator_id_refs_id_cff6fecf" FOREIGN KEY ("creator_id") REFERENCES "myuser" ("id") DEFERRABLE INITIALLY DEFERRED;
The error was: relation "myuser" does not exist

Error in migration: anotherapp:0001_initial
DatabaseError: relation "myuser" does not exist

有什么想法吗?

这个问题有一个(已关闭的)工单(http://south.aeracode.org/ticket/390),其中讨论了这个主题。 - Amir Ali Akbari
1个回答

11

这里似乎存在着一个真正的循环依赖关系。但你可以很容易地打破它:将在MyApp中创建的m2m表格移动到单独的迁移中。最简单的方法可能是将0001_initial.py复制到一个新名称,然后从原始文件中删除m2m表格的块(前向和后向!),并从副本中删除其他所有内容。

该副本应命名为使其在0001_initial和0002_whatever之间排序 - 比如说,0001_initial2.py;它应该依赖于("AnotherApp","0001_initial") - 而这又依赖于("MyApp","0001_initial")。


1
太棒了!我最终在MyApp的第一个迁移中使用了needed_by,然后在MyApp的第二个迁移中使用了depends_on。这样我就能够设置依赖关系,而不必修改在其他地方开发的AnotherApp。 - askvictor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接