Django "InconsistentMigrationHistory" in CI
Django's django_migrations table records a migration as applied before one of its declared dependencies. The applied history violates the dependency graph, so Django raises InconsistentMigrationHistory. This is deterministic.
What this error means
migrate fails with "InconsistentMigrationHistory: Migration app.0003 is applied before its dependency app.0002". It typically appears when CI reuses a database whose applied history does not match the current dependency graph.
django.db.migrations.exceptions.InconsistentMigrationHistory:
Migration orders.0003_status is applied before its dependency
orders.0002_index on database 'default'.Common causes
Reused database with stale applied history
A shared/persisted CI database carries an applied history that conflicts with reordered or merged migrations on this branch.
Dependencies edited after applying
Changing a migration's dependencies after it was applied makes the recorded order inconsistent with the new graph.
How to fix it
Use a clean database per CI run
Apply the full migration set to a fresh database so the applied order matches the dependency graph.
services:
db:
image: postgres:16 # fresh DB each run
# step:
python manage.py migrate --noinputReconcile the dependency order
- Inspect the conflicting migrations'
dependencies. - Restore a consistent order or add a merge migration.
- Avoid editing dependencies of already-applied migrations.
How to prevent it
- Run CI migrations against an ephemeral database.
- Do not edit dependencies of applied migrations; add new ones instead.
- This is deterministic - retrying against the same database fails identically.