Django "InconsistentMigrationHistory" in CI
Django compares the migrations recorded in django_migrations against the dependency graph in code and finds a contradiction: a migration is marked applied before one it depends on. This usually means a reused or partially migrated database, not a fresh CI one.
What this error means
migrate fails with "django.db.migrations.exceptions.InconsistentMigrationHistory: Migration app.0003_x is applied before its dependency app.0002_y on database 'default'."
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration
app.0003_add_index is applied before its dependency app.0002_rename on database 'default'.Common causes
A reused database with stale migration records
CI cached or reused a database (or a volume) whose django_migrations table reflects an older, reordered graph.
Migrations reordered or squashed after being applied
Dependencies were changed or migrations squashed after some were already recorded, so history no longer matches the graph.
How to fix it
Start from a fresh database in CI
- Do not reuse a persisted database or volume across runs.
- Let the service container create a clean database each job.
- Run migrate against the fresh database so history matches the graph.
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: app_test
POSTGRES_PASSWORD: postgres
ports: ["5432:5432"]Fix dependency ordering in the migrations
Ensure each migration declares the correct dependencies so the graph and recorded history agree.
class Migration(migrations.Migration):
dependencies = [("app", "0002_rename")]How to prevent it
- Use a fresh database each CI run rather than a reused volume.
- Do not reorder or squash migrations that have already been applied elsewhere.
- Keep migration dependencies accurate when editing the graph.