phourxx
phourxx

Reputation: 621

how to configure django to run migration files in a specific order

I have a project which requires me to insert initial data into the database on migration. Now everything works fine except that I need to tell django to insert some specific data linked with a migration file before running another.

For example, lets say I have two migration files, A and B, and each is attached to models ModelA and ModelB respectively. The migration for A works fine, but for B, I am automatically generating its SQL statement which requires me to use ModelA.objects.get(id=id) while generating the statements, but I get an error ModelA.DoesNotExist, which means migration A hasn't been saved.

Is there a way I can ensure the data inserted by the migration A has been saved before proceeding to run migration B?

Upvotes: 0

Views: 309

Answers (1)

Alex Carlos
Alex Carlos

Reputation: 902

As @tom-dalton mentioned the way to go is through dependencies.

You can see an example of a dependency in a migration file here in the docs.

They are written in the following format:

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [("your_app_name", "migration_file_name")]

    operations = [
        # Migration operations here
    ]

You can find the migration file name by looking in the migrations folder of your project.

Finally you need to follow the data migration process to access ModelA data in your new migration.

Upvotes: 1

Related Questions