Django manage.py: Migration applied before its dependency

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


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.


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;