55

When running python manage.py migrate I encounter this error:

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration
<appname>.0016_auto_<date2>_<time2> is applied before its dependency
<appname>.0001_squashed_0015_auto_<date1>_<time1>

running python manage.py showmigrations returns:

<appname>
 [X] 0001_squashed_0015_auto_<date1>_<time1> (15 squashed migrations)
 [X] 0016_auto_<date2>_<time2>
 [ ] 0017_<modelname>_squashed_0019_auto_<date3>_<time3> (3 squashed migrations)

I was trying out django-extensions yesterday, when it all got messed up after me running some direct SQL queries and I reset hard using git. I'm still learning about migrations, so I don't understand what is wrong, since it seems to me that both migrations already have been applied.

Thank you for your help!

Ricardo
  • 3,696
  • 5
  • 36
  • 50
Matthias Michael Engh
  • 1,159
  • 2
  • 10
  • 25

12 Answers12

68

This worked for me. I thank my coworker for sharing this knowledge after I searched online for many hours.

Start your db shell

python manage.py dbshell

Use the database you want. If you don't know, run .databases (SQLite) or SHOW databases

mysql>use <database_name>;

Retrieve all the migrations under your app

mysql> select * from django_migrations where app='<app>';

You will see the output with ids next to all migrations. You may want to drop the migration that was applied before its dependency: <appname>.0016_auto_<date2>_<time2>. Say its id is 361:

mysql> delete from django_migrations where id=361;

Now out of the db shell, run python manage.py migrate again.

Ricardo
  • 3,696
  • 5
  • 36
  • 50
user3661888
  • 860
  • 1
  • 7
  • 9
  • 3
    Great answer. a more simple way to find the migration file to drop is: delete from django_migrations where name= – Baruch Gans May 01 '22 at 08:54
14

I have solved this problem when i did (custom user model) by this steps:

  1. delete this file : migrations\0001_initial.py

  2. delete this : db.sqlite3

  3. put this code in settings.py : AUTH_USER_MODEL = 'users.CustomUser'

  4. Then do (makemigrations) then (migrate )

  5. run server .. the problem solved :)


i have used this link it is help me to solve the problem of dependency :

https://docs.djangoproject.com/en/3.1/topics/auth/customizing/

Due to limitations of Django’s dynamic dependency feature for swappable models, the model referenced by AUTH_USER_MODEL must be created in the first migration of its app (usually called 0001_initial); otherwise, you’ll have dependency issues.

In addition, you may run into a CircularDependencyError when running your migrations as Django won’t be able to automatically break the dependency loop due to the dynamic dependency. If you see this error, you should break the loop by moving the models depended on by your user model into a second migration. (You can try making two normal models that have a ForeignKey to each other and seeing how makemigrations resolves that circular dependency if you want to see how it’s usually done.)

MAY
  • 213
  • 2
  • 4
11

You have squashed the migrations, so one of the dependencies that 0016_auto_<date2>_<time2> had is now part of the newly created squashed migrations. Meanwhile the 0016_auto_<date2>_<time2> has already been run and now you're trying to run the squashed migration.

I personally don't know if there's any way to fix this automatically. You will need to fix the issues yourself. If you have version control, revert these changes and try to rethink how you should squash the migration without affecting old ones.

masnun
  • 11,635
  • 4
  • 39
  • 50
  • 2
    Yeah, I figured something similar. Thanks for the answer. I did fix it just now by deleting the squashed data, deleting the migration files that had dependencies on the squashed data and run "makemigrations" and "migrate". This is something that I think should be fixed in the django project. – Matthias Michael Engh Aug 17 '16 at 12:58
  • Hi brother @masnun, I have the same problem. In my first migration file I have, dependencies = [ ('custom_users','__latest__'), ('custom_devices', '0008_remove_customdevice_os_version'), ] I have the error for the first dependencies. Should I delete this ? – Harun-Ur-Rashid Nov 14 '17 at 05:13
  • Check database state and may be use SQL to manually set things up. – masnun Nov 14 '17 at 18:53
  • 1
    Hi, I have the same problem, and fixed that by search in the database django_migrations table, and deleting the row corresponding to the migration with problems, then apply the migrations in order. – Jaime Dec 06 '18 at 14:14
6

run this python manage.py dbshell

INSERT INTO public.django_migrations(app, name, applied)
VALUES ('YOUR_APP_NAME, '0017_<modelname>_squashed_0019_auto_<date3>_<time3>', now());

and you should be fine. If Your migration was changing a lot to the database, then I am afraid it won't be that easy to fix it.

test30
  • 3,496
  • 34
  • 26
2
  1. Edit the dependencies of the conflicting migration, so that it no longer references the already applied migration.
  2. Then run python manage.py migrate again and it should be fixed.

    • Warning: this only work suppossing that the state of the database matchs the state you get having applied the conflicting migration.
Duilio
  • 876
  • 1
  • 10
  • 15
2

you need to fake migrations and migrate again just make sure that you have a backup from your data because when you migrate again you need to delete apps table. make sure that you look at show migrations and migrate un migrated apps by its sequence

user907988
  • 625
  • 1
  • 5
  • 17
0

I had the same issue on 2020 with Django 3.0.6. I tried all the relevant answers with no success. So I went in my database and deleted all the tables. You must export the relevant tables if you have done lot of work. I mainly delete django files in my database. And after, run:

python manage.py makemigrations <my-app>

And:

python manage.py migrate

Export your relevant tables if any.

Alexander
  • 2,174
  • 3
  • 16
  • 25
oklm
  • 135
  • 1
  • 10
0
  1. First back up your database before resolving the conflicts, (Use "python manage.py dumpdata > db.json" for SQLite).
  2. Execute python manage.py dbshell, to access the database.
  3. Delete the migrations rows that are having conflicts from the django_migrations table.
  4. Rename the tables conflicting in the database
  5. Execute the makemigrations and migrate commands
  6. After successful migrations, Drop the newly readded tables and finally restore the previously renamed tables to match the migrations need
NINSIIMA WILBER
  • 159
  • 1
  • 7
0

I had the same problem, and here's how I solved it.

The following is my error message

File "/usr/local/lib/python3.11/site-packages/django/db/migrations/loader.py", line 327, in check_consistent_history
    raise InconsistentMigrationHistory(
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration aaaa.0024_campaign_template is applied before its dependency bbbb.0005_templatemodel_from_template on database 'default'.

My solution

python manage.py migrate bbbb 

python manage.py migrate

Because I changed the Django's app name in batches, the application order was not consistent when applied to the database. The bbbb that aaaa relies on was not created first, so I manually created the bbbb first

jason
  • 88
  • 6
0

Migration file is not created for all app:
step 1:

create migration folder and add __init__.py file for all app

step 2:

delete db.sqlite3 database 

step 3:

python manage.py migrate
python manage.py makemigrations 
Shaiful Islam
  • 335
  • 2
  • 12
-2
  • Delete all of your migrations folder
  • Delete the database(sqlite3)

Then run the makemigrations and migrate command

Manas Paul
  • 86
  • 1
  • 9
-9
  1. Delete the migration files.

  2. Run:

    python manage.py migrate
    python manage.py makemigrations 
    python manage.py migrate
    python manage.pyrunserver
    
double-beep
  • 5,031
  • 17
  • 33
  • 41
Mohamed
  • 15
  • 4
  • 2
    This is only a solution in a development / local environment and not a solution for most uses of this feature. – Rohit Jul 31 '20 at 19:03
  • 1
    additionally, `makemigrations` would need to be run before `migrate`. Missing a space before `runserver` – axon Apr 16 '21 at 10:14