Skip to content
Latchkey

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.

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

.github/workflows/ci.yml
services:
  db:
    image: postgres:16   # fresh DB each run
# step:
python manage.py migrate --noinput

Reconcile the dependency order

  1. Inspect the conflicting migrations' dependencies.
  2. Restore a consistent order or add a merge migration.
  3. 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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →