Scott Deerwester
Scott Deerwester

Reputation: 3977

Django migration dependency order

I have a relatively complex set of Django models. I'm trying to start with a fresh set of migrations (rm -rf apps/*/migrations; bin/dev/manage.py makemigrations A B C...). makemigrations works fine, and there are no circular dependencies, but I'm consistently getting an InconsistentMigrationHistory exception when I migrate. Here's a graph of the dependencies between the migrations, simplified to remove the migrations with no related dependencies, and with the app names redacted for readability: Migration dependencies The links in red cause the error (different ones each time I run migrate), even with a run_before added to each migration that should be run before its dependency:

A/migrations/0002_whatever.py:

...
run_before = [('P', '0001_initial'),]

Here's the error text.

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration `P.migrations.0001_initial` is applied before its dependency `A.migrations.0002_whatever` on database 'default'.

Any ideas?

Upvotes: 4

Views: 2370

Answers (1)

Sidhin S Thomas
Sidhin S Thomas

Reputation: 885

You need to reset the database as well.

When you created new migrations without resetting the database, the sync between your database and migrations was lost.

So when you try to migrate the new migrations, Django will find it inconsistent and fail.

It is generally not a good idea to clear all migrations and generate new migrations for the same reason.

Imagine this happening in production.

Upvotes: -1

Related Questions