What's Actually Happening

Django migrations conflict when multiple developers create migrations in parallel branches. Applying migrations fails with dependency errors.

The Error You'll See

```bash $ python manage.py migrate

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration myapp.0002_add_field is applied before its dependency myapp.0001_initial ```

Why This Happens

  1. 1.Parallel development - Multiple migration branches
  2. 2.Wrong merge order - Migrations merged incorrectly
  3. 3.Dependency conflict - Circular or missing dependencies
  4. 4.Applied out of order - Migrations applied in wrong sequence

Step 1: Check Migration Status

```bash # List migrations: python manage.py showmigrations

# Check migration plan: python manage.py migrate --plan

# Check squashed: python manage.py showmigrations --list ```

Step 2: Rollback Conflicting Migration

```bash # Rollback to previous: python manage.py migrate myapp 0001

# Fake migration: python manage.py migrate --fake myapp 0002 ```

Step 3: Merge Migrations

```bash # Create merge migration: python manage.py makemigrations --merge

# Or manually create: python manage.py makemigrations myapp --name merge_migrations ```

Step 4: Fix Dependencies

python
# In migration file:
dependencies = [
    ('myapp', '0001_initial'),
    ('otherapp', '0003_add_field'),
]

Django Migration Checklist

CheckCommandExpected
Show migrationsshowmigrationsAll applied
No conflictsmigrate --planNo errors
Dependenciescheck filesCorrect

Verify the Fix

bash
python manage.py migrate
# Output: No migrations to apply
  • [Fix Django Migration Apply Failed](/articles/fix-django-migration-apply-failed)
  • [Fix Django Model Not Found](/articles/fix-django-queryset-n-plus-one)