[Django]-Django manage.py: Migration applied before its dependency

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.

71👍

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.

16👍

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

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.

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.

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

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.

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

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

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 

-2👍

  • Delete all of your migrations folder
  • Delete the database(sqlite3)

Then run the makemigrations and migrate command

-9👍

  1. Delete the migration files.

  2. Run:

    python manage.py migrate
    python manage.py makemigrations 
    python manage.py migrate
    python manage.pyrunserver
    

Leave a comment