Django manage.py: migration applies before its dependency

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 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) 

Yesterday I tried django extensions, when it all went wrong after I made a few direct SQL queries and I completely reset the settings using git. I am still studying migration, so I donโ€™t understand whatโ€™s the matter, since it seems to me that both migrations have already been applied.

Thanks for the help!

+21
source share
5 answers

You crushed the migrations, so one of the dependencies that 0016_auto_<date2>_<time2> had now is part of the newly created compressed migrations. Meanwhile, 0016_auto_<date2>_<time2> already running, and now you are trying to start the compressed migration.

I personally do not know if there is a way to fix this automatically. You will need to fix the problems yourself. If you have version control, revert these changes and try to rethink how you should cross-migrate without affecting the old ones.

+8
source

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 has changed a lot in the database, then I'm afraid it will not be so easy to fix.

0
source
  1. Change the dependencies of the conflicting migration so that it no longer refers to the migration that has already been applied.
  2. Then run python manage.py migrate again and this should be fixed.

    • Warning: this only works if the state of the database matches the state that you received by applying conflicting migration.
0
source

It worked for me. I thank my colleague for sharing this knowledge after searching the Internet for many hours.

Run your db python manage.py dbshell shell

Use the database you want. If you do not know, run "show database" mysql> use;

Get all migrations under your application mysql> select * from django_migrations, where app = '';

You will see an output with identifiers next to all migrations. Look at the migration you want to drop. Let's say id: 361 mysql> delete from django_migrations, where id = 361;

0
source

It worked for me. I thank my colleague for sharing this knowledge after searching the Internet for many hours.

Start your DB shell

 python manage.py dbshell 

Use the database you want. If you donโ€™t know, run Show Databases

 mysql>use <database_name>; 

Get all migrations under your application

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

You will see an output with identifiers next to all migrations. Look at the migration you want to drop. Say id 361

 mysql> delete from django_migrations where id=361; 
0
source

All Articles