Django Migrations Solving migration conflicts

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!



Sometimes migrations conflict, resulting in making the migration unsuccesful. This can happen in a lot of scenerio's, however it can occur on a regular basis when developing one app with a team.

Common migration conflicts happen while using source control, especially when the feature-per-branch method is used. For this scenario we will use a model called Reporter with the attributes name and address.

Two developers at this point are going to develop a feature, thus they both get this initial copy of the Reporter model. Developer A adds an age which results in the file file. Developer B adds a bank_account field which resulsts in 0002_reporter_bank_account. Once these developers merge their code together and attempt to migrate the migrations, a migration conflict occurred.

This conflict occurs because these migrations both alter the same model, Reporter. On top of that, the new files both start with 0002.

Merging migrations

There are several ways of doing it. The following is in the recommended order:

  1. The most simple fix for this is by running the makemigrations command with a --merge flag.

    python makemigrations --merge <my_app>

    This will create a new migration solving the previous conflict.

  2. When this extra file is not welcome in the development environment for personal reasons, an option is to delete the conflicting migrations. Then, a new migration can be made using the regular makemigrations command. When custom migrations are written, such as migrations.RunPython, need to be accounted for using this method.

Got any Django Question?