SS_Rebelious
SS_Rebelious

Reputation: 1364

How to fix order of Django migrations?

I have several apps in my Django 2.0.2 project. One of the apps (app_one) has a migration that depends on migrations from the other app (app_two) being installed. It is initial migration of app_one and it looks like this:

def import_data(apps, schema_editor):
    schema_model = apps.get_model('app_two', 'Model')
    # do import

def drop_data(apps, schema_editor):
    schema_model = apps.get_model('app_two', 'Model')
    # undo import

class Migration(migrations.Migration):

    dependencies = [
        ('app_two', '0005_auto_20180127_2155')
    ]

    operations = [
        migrations.RunPython(
            import_data, 
            reverse_code=drop_data
    )
]

Currently in order to install the database I need to manage.py migrate app_two and only after that do manage.py migrate, because otherwise I get an error on this migration that relation Model from app_two does not exist. Also I'm unable to run tests manage.py test normally due to the same error. It seems that Django ignores dependency in this migration for some reason. How do I fix that?

Upvotes: 3

Views: 4705

Answers (3)

SS_Rebelious
SS_Rebelious

Reputation: 1364

IDK wat's happening, but today I made some changes to migrations like adding run_before and the issue was gone. BUT! I rolled all changes back, droped all the related databases and the bug didn't came back. The code is literally the same as it was when I posted the question...

Upvotes: 1

Dino Perovic
Dino Perovic

Reputation: 123

Check out the docs about ordering your migrations:

https://docs.djangoproject.com/en/2.0/howto/writing-migrations/#controlling-the-order-of-migrations

Upvotes: 3

Brian Ocampo
Brian Ocampo

Reputation: 1515

Take a look at the Django documentation regarding the configuration variable MIGRATION_MODULES.

Link to doc: https://docs.djangoproject.com/en/2.0/ref/settings/#std:setting-MIGRATION_MODULES

Upvotes: 0

Related Questions