Search code examples
pythondjangopython-3.xmigrationdatabase-migration

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?


Solution

  • 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...