Django manage.py: Migration applied before its dependency

别等时光非礼了梦想. 提交于 2019-12-23 06:48:09

问题


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)

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!


回答1:


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.




回答2:


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.




回答3:


  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.



回答4:


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 "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. Look at the migration you want to drop. Say the id is 361

mysql> delete from django_migrations where id=361;



回答5:


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



来源:https://stackoverflow.com/questions/38996599/django-manage-py-migration-applied-before-its-dependency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!