Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What migration order does South follow across different apps?

I've recently begun using South for migrations in my Django project. All was going well until recently when I ran into a peculiar issue.

I have two apps in my project, say, App-A and App-B. A model in App-A has a foreign key to a model in App-B. When I've been trying to build my system, I ran syndb which created all the auth_ and the south_ tables. Then I ran migrate which threw up errors. When it tried creating the model from App-A, which referenced a model from App-B, the model App-B wasn't migrated/created as yet and therefore the error.

In order to resolve this, I had to manually migrate App-B first and then App-A. Am i doing something wrong here? How is South supposed to know the migration order across apps?

Thanks.

like image 522
Mridang Agarwalla Avatar asked Sep 19 '11 17:09

Mridang Agarwalla


People also ask

How does migration work in Django?

Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They're designed to be mostly automatic, but you'll need to know when to make migrations, when to run them, and the common problems you might run into.

What is the difference between Makemigrations and migrate in Django?

So the difference between makemigrations and migrate is this: makemigrations auto generates migration files containing changes that need to be applied to the database, but doesn't actually change anyhting in your database. migrate will make the actual modifications to your database, based on the migration files.


2 Answers

This explained it https://south.readthedocs.io/en/latest/dependencies.html.

Migrations for apps are nice ‘n all, but when you start writing a large project, with a lot of apps, you realise you have foreign key relationships between apps and working out what order migrations would need 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 another app having run a certain migration first; for example, if my app “forum” depends on the “accounts” app having created its user profile table, 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 least up to 0003_add_user_profile, and if not will migrate it for you.

Dependencies also work in reverse; South knows not to undo that 0003_add_user_profile migration until it has undone the 0002_post migration.

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 each other) No upwards dependencies in the same app (so you can’t make 0002_post in the forum app depend on 0003_room in the same app, either directly or through a dependency chain.

like image 124
Mridang Agarwalla Avatar answered Oct 16 '22 02:10

Mridang Agarwalla


South migrates apps in the order they appear in the INSTALLED_APPS tuple in settings.py. So just make sure App-B comes before App-A in your settings.py, and it should work :)

like image 12
haeric Avatar answered Oct 16 '22 02:10

haeric