Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django manage.py: Migration applied 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)

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!

like image 668
Matthias Michael Engh Avatar asked Aug 17 '16 12:08

Matthias Michael Engh


3 Answers

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;
like image 78
user3661888 Avatar answered Nov 01 '22 10:11

user3661888


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.

like image 11
masnun Avatar answered Nov 01 '22 09:11

masnun


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

like image 11
MAY Avatar answered Nov 01 '22 10:11

MAY