Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django south circular dependency

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?

like image 648
askvictor Avatar asked Jul 17 '13 22:07

askvictor


1 Answers

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").

like image 106
Shai Berger Avatar answered Nov 01 '22 16:11

Shai Berger