问题描述
我有一个应用程序(让我们称之为MyApp)在Django 1.5项目中。 MyApp定义了一个自定义用户模型(MyUser)。该项目使用另一个引用MyUser的应用程序(AnotherApp)。 MyApp引用AnotherApp中的字段。我的开发笔记本电脑上的一切工作都很好。我试图在服务器上部署我的项目,当我进入迁移步骤时,MyApp由于依赖于AnotherApp而失败,AnotherApp在依赖MyApp时失败(我已经尝试独立迁移应用程序)。两者都在他们各自的首次迁移失败(0001)
运行myapp迁移:
- 向前移动到0017_auto__blah_blah。
> myapp:0001_initial
致命错误 - 以下SQL查询失败:ALTER TABLEmyapp_mymodel_othermodelADD CONSTRAINTothermodel_id_refs_id_ae052c6dFOREIGN KEY(othermodel_id)REFERENCESanotherapp_othermodel(id)DEFERRABLE INITIALLY DEFERRED;
错误是:relationanotherapp_othermodel不存在
迁移错误:myapp:0001_initial
DatabaseError:relationanotherapp_othermodel不存在
为anotherapp运行迁移:
- 向前迁移到0008_blah_blah。
> anotherapp:0001_initial
致命错误 - 以下SQL查询失败:ALTER TABLEanotherapp_othermodelADD CONSTRAINTcreator_id_refs_id_cff6fecfFOREIGN KEY(creator_id)REFERENCESmyuser(id)DEFERRABLE INITIALLY DEFERRED;
错误是:关系myuser不存在
迁移错误:anotherapp:0001_initial
DatabaseError:关系myuser不存在
任何想法?
在这里似乎有一个真正的循环依赖。尽管如此,您可以轻松地打破它:将MyApp中的m2m表的创建移动到单独的迁移。最简单的方法是将0001_initial.py复制到一个新名称,然后删除原始的m2m表(向前和向后!)的块,并删除副本中的所有其他内容。该副本应该命名为0001_initial和0002_whatever之间的命令,比如说0001_initial2.py;
而且它应该依赖于(AnotherApp,0001_initial) - 而这依赖于(MyApp,0001_initial)。
I have an app (let's call it MyApp) in a Django 1.5 project. MyApp defines a custom user model (MyUser). The project uses another app (AnotherApp) which references MyUser. MyApp references fields in AnotherApp.
Everything has been working fine on my development laptop. I'm attempting to deploy my project on a server, and when I come to the migrate step, MyApp fails due to a dependency to AnotherApp, and AnotherApp fails on a dependency to MyApp (I have tried migrating the apps independently). Both are failing on their respective first migrations (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
Any ideas?
There appears to be a real circular dependency here. You can break it quite easily, though: Move the creation of the m2m table in MyApp to a separate migration. The easiest way to do so, probably, is to copy 0001_initial.py to a new name, then remove the blocks for the m2m table (forwards and backwards!) in the original, and remove everything else in the copy.
The copy should be named so that it is ordered between 0001_initial and 0002_whatever -- say, 0001_initial2.py; and it should depend on ("AnotherApp", "0001_initial") -- which, in turn, should depend on ("MyApp", "0001_initial").
这篇关于Django南圆依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!