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 0002_reporter_age.py
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.
There are several ways of doing it. The following is in the recommended order:
The most simple fix for this is by running the makemigrations command with a --merge flag.
python manage.py makemigrations --merge <my_app>
This will create a new migration solving the previous conflict.
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.