14

With Django 1.11.22 I'm trying to run migrations

python manage.py migrate
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration base.0036_auto_20190227_1226 is applied before its dependency base.0027_auto_20170801_1228_squashed_0037_auto_20190222_1347 on database 'default'.

My first try to solve this was

sudo -u postgres psql -d albatros -c \
"DELETE FROM django_migrations WHERE name = '0036_auto_20190227_1226' AND app = 'base'"

In the hope of deleting the migration from the migration table would fix it. Unfortunately I'm now getting:

CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0037_auto_20190222_1347, 0036_auto_20190227_1226 in base).
To fix them run 'python manage.py makemigrations --merge'

When tryin makemigrations --merge it does not find any migrations to merge. This is what showmigrations looks like:

 ./manage.py  showmigrations base
base
 [X] 24_initial
 [X] 24_to_26
 [X] 26_to_27
 [X] 0027_auto_20170801_1228
 [X] 0028_resourcebase_is_approved
 [X] 0029_auto_20171114_0341
 [X] 0030_auto_20180309_0833
 [X] 0031_auto_20180309_0837
 [X] 0032_auto_20180329_1844
 [X] 0033_auto_20180330_0951
 [X] 0034_auto_20180606_1543
 [X] 0035_resourcebase_dirty_state
 [ ] 0036_auto_20190227_1226
 [ ] 0036_auto_20190129_1433
 [ ] 0037_auto_20190222_1347

Can one say how to correctly apply the migrations and solve the multiple leaf nodes error?

Anatol
  • 1,923
  • 6
  • 26
  • 55
  • This answer on how to fix the migration manually helped me: https://stackoverflow.com/a/48131844/1474777 – Anatol Jul 14 '19 at 11:16

4 Answers4

11

In this 2 migration files (0037_auto_20190222_1347, 0036_auto_20190227_1226) you have same dependencies, check them. They seems like a list with tuple in it

dependencies = [
    ('round', '0008_auto_20200116_0752'),
]

You need to manually write "0036_auto_20190227_1226" into 0037_auto_20190222_1347 file dependencies variable.

Tryph
  • 5,946
  • 28
  • 49
  • 5
    Your answer is not easy to validate. Try giving a little more background information, e.g. how you came to the conclusion, in cases like this. – Chris Jan 29 '20 at 18:20
  • 1
    For others wondering what to put actually in the first element of the tuple, the comment in Django's code for `dependencies` is interesting: `# Other migrations that should be run before this migration. Should be a list of (app, migration_name).` – Sander Vanden Hautte Oct 23 '21 at 10:44
2

each django migration contains a dependency referring to the migration before it, as if it were a breadcrumb, this is controlled through the files that are in the migrations folder as well as through the database, in the django_migrations table. Each migration file has a line something like this:

dependencies = [
     ('round', '0008_auto_20200116_0752'),
]

where the second parameter of the tuple must be exactly the name that must be in the database in the django_migrations table. That way the tree cannot have loose nodes, make sure your database in the django_migrations table is consistent with the migration sequence of each file through the dependencies:

dependencies = [
     ('round', '0008_auto_20200116_0752'),
]
Marcos Paolo
  • 301
  • 2
  • 7
0

An alternative to resolve this would be to use django-migration-fixer

Fixing migrations on your dev branch can be done using

$ git checkout [dev-branch]
$ git merge [main/master]

Follow the installation instructions here

Run

$ python manage.py makemigrations --fix -b [main/master]

commit the changes and push to the remote branch

$ git add .
$ git commit -am ...
$ git push ...
jackotonye
  • 3,537
  • 23
  • 31
-2

You can merge your migration and do migrate

(venv)yourprj$python manage.py makemigrations --merge
(venv)yourprj$python manage.py migrate
7guyo
  • 3,047
  • 1
  • 30
  • 31
  • 4
    Generally, you don't want to merge migrations, in case you need to back out just one, or just the other. They are separate changes, and should remain separate – GreenAsJade Oct 23 '21 at 05:32
  • imho, merging migrations should be done with care. If parts of them are already executed on some of your installations (assuming there is more than one), you risk double-executing them with the newly created merged migration file. – Blafasel42 May 02 '22 at 07:26