Handling Django Migration Conflicts Effectively
In this article
To handle conflicts in Django database migrations effectively, you must first identify the conflicting migration files, then resolve them by merging or rebasing your migration graph so that each app has a single linear history. This typically involves using makemigrations --merge to create a merge migration, or manually editing dependencies to reconcile divergent branches.
Why Migration Conflicts Happen
Django tracks migrations as a directed acyclic graph (DAG) per app. Each migration file has a dependencies list that points to the migration(s) it builds upon. When two developers create new migrations from the same parent migration in separate branches, you get two children with the same parent—a classic conflict. This is common in team environments where feature branches run makemigrations independently. The conflict appears when you try to apply migrations or run makemigrations --check in CI, because Django sees multiple leaf nodes with no clear linear order.
How Django Detects Conflicts
Django’s migration loader builds the graph at runtime. If an app has more than one migration with no path between them (i.e., two leaves not connected by dependencies), it raises an InconsistentMigrationHistory error. For example, if 0002_feature_a and 0002_feature_b both depend on 0001_initial, the loader cannot determine which should come first. The error message usually lists the conflicting migrations and suggests running makemigrations --merge. Note that conflicts can also occur across apps if a migration in one app depends on a migration in another that has been altered or deleted.
Step-by-Step Resolution Workflow
- Run
python manage.py makemigrations --check --dry-runto see if there are pending model changes that might complicate the merge. - Run
python manage.py makemigrations --merge. Django will prompt you to create a merge migration that depends on both conflicting migrations. - Review the generated merge migration file. It will have an empty
operationslist and adependencieslist containing both branches. - Apply the merge migration with
python manage.py migrate. - If the merge creates a new migration name like
0003_merge_20250115, commit it to version control and ensure all team members pull it before creating new migrations.
If --merge fails because of actual model conflicts (e.g., both branches added a field with the same name but different types), you must manually edit one of the migration files to align with the other, or delete one and recreate it after merging the model code.
Manual Conflict Resolution: Editing Dependencies
Sometimes you can resolve a conflict by editing the dependencies list of one migration to point to the other. For instance, if 0002_feature_a and 0002_feature_b both depend on 0001_initial, and you know feature_a should come first, you can change 0002_feature_b's dependencies to include ('app', '0002_feature_a') instead of ('app', '0001_initial'). This creates a linear chain. However, this only works if the two migrations do not touch the same model fields or tables in incompatible ways. If they do, you must manually merge the operations into a single migration, which is more error-prone but sometimes necessary when the changes are deeply intertwined.
Preventing Conflicts in Team Workflows
- Encourage developers to run
makemigrationsonly when they have pulled the latestmainbranch and to commit migration files immediately after creation. - Use a single shared database for development (or a consistent migration baseline) so that everyone sees the same migration history.
- Integrate
makemigrations --check --dry-runinto CI to catch missing or conflicting migrations early. - Consider using a tool like
django-migration-conflict(a third-party package) that detects conflicts before they reach the database, but rely on Django’s built-in merge as the primary solution. - Rebase feature branches frequently onto the latest
mainto minimize the time window for conflicts.
Common Pitfalls and Misconceptions
A frequent mistake is deleting a migration file to “reset” the history. This breaks the graph for anyone who has already applied that migration, leading to InconsistentMigrationHistory errors on their machines. Instead, create a new migration that reverses the unwanted changes. Another misconception is that migrate --fake resolves conflicts—it only marks migrations as applied without running them, which can mask issues and cause database schema drift. Also, remember that migration conflicts are not the same as model field conflicts; the latter require code changes, not just graph merging. Finally, if you use squashed migrations, be aware that squashing can introduce new conflicts if the squashed migration is not aligned with the current graph.
When to Use Merge vs. Rebase vs. Reset
Use makemigrations --merge when both branches are valid and you want to preserve both histories. Use a rebase (manually editing dependencies) when you want a single linear history and the changes are independent. Use a reset (deleting and recreating migrations) only in early development before anything is deployed, and only if you coordinate with all team members to clear their local databases. In production, never reset; always add a new migration to reverse or adapt. The choice depends on your team’s preference for linear history (rebase) versus preserving branch context (merge).
Frequently Asked Questions
What does “conflicting migrations detected” mean?
It means Django found two or more migrations in the same app that both depend on the same parent migration, creating a fork in the migration graph. The loader cannot determine which one to apply first, so it asks you to resolve the ambiguity.
Can I just delete one of the conflicting migrations?
Only if that migration has never been applied to any database (including your local dev database) and you can safely recreate it after merging the model changes. If it has been applied, deleting it will cause errors on machines that already have it recorded in the django_migrations table.
Why does makemigrations --merge create an empty migration?
Because the merge migration’s only purpose is to unify the graph—it doesn’t change the database schema. Its operations list is empty, and its dependencies point to both conflicting branches, telling Django that both are prerequisites.
How do I avoid conflicts when multiple developers work on the same app?
Adopt a workflow where you pull the latest code before running makemigrations, commit migration files immediately, and run makemigrations --check in CI. Also, consider using per-app migrations only when necessary, and keep model changes small and frequent.
What if the merge migration fails due to model field conflicts?
You need to manually edit the migration files to reconcile the conflicting operations. For example, if both added a field named slug with different max_length, you must decide on one and modify the other migration to match, or combine them into a single migration.
Is it safe to use migrate --fake to skip a conflicting migration?
No. --fake records the migration as applied without running its operations, which can leave your database schema out of sync with the model code. It should only be used in specific recovery scenarios, not as a regular conflict resolution tool.