When I run python manage.py migrate
on my Django project, I get the following error:
Traceback (most recent call last):
File \"manage.py\", line
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'.
So we can migrate database without admin(admin.0001_initial) firstly.
After its dependency migrated, execute commands to migrate admin.0001_initial
.
Python manage.py makemigrations appname
Python manage.py migrate appname
Python manage.py makemigrations appname
Python manage.py migrate appname
Solved by commenting this before migration in settings.py
'django.contrib.admin'
and in urls.py,
('admin/', admin.site.urls)
.
uncomment after migrate
This Problem will come most of the time if you extend the User Model post initial migration. Because whenever you extend the Abstract user it will create basic fields which were present un the model like email, first_name, etc.
Even this is applicable to any abstract model in django.
So a very simple solution for this is either create a new database then apply migrations or delete [You all data will be deleted in this case.] the same database and reapply migrations.
Before performing any other steps, back up your database. Then back it up again.
Remove any custom user model code out of the way, disable your custom model and app in settings, then:
python manage.py dumpdata auth --natural-primary --natural-foreign > auth.json
python manage.py migrate auth zero # This will also revert out the admin migrations
Then add in your custom model, set it in settings, and re-enable the app. Make sure you have no migrations on this app yet.
Then:
python manage.py makemigrations <your-app>
python manage.py migrate
python manage.py loaddata auth.json # Assumes your user-model isn't TOO dissimilar to the standard one.
Done!
There is another reason besides user error that can lead to this sort of problem: a known issue with Django when it comes to squashed migrations.
We have a series of migrations that work perfectly fine in Python 2.7 + Django 1.11. Running makemigrations
or migrate
always works as it should, etc., even (for the purpose of testing) when the database is freshly re-created.
However, as we move a project to Python 3.6 (currently using the same Django 1.11) I've been stuck trying to figure out why the same migrations apply just fine only the first time they are run. After that, any attempt to run makemigrations
or even just migrate
results in the error:
django.db.migrations.exceptions.InconsistentMigrationHistory
wherein migration foo.0040-thing
is applied before its dependency foo.0038-something-squashed-0039-somethingelse
(we only happen to have that one squashed migration... the rest are much more straightforward).
What's bugged me for a while is why this only happens on Python 3. If the DB is truly inconsistent this should be happening all the time. That the migrations appear to work perfectly fine the first time they are applied was equally confounding.
After much searching (including the present Q&A thread), I stumbled upon the aforementioned Django bug report. Our squash migration did indeed use the b
prefix in the replaces
line (e.g., replaces = [(b'', 'foo.0038-defunct'),.......]
Once I removed the b
prefixes from the replaces
line it all worked normally.
Your django_migrations table in your database is the cause of inconsistency and deleting all the migrations just from local path won't work.
You have to truncate the django_migrations table from your database and then try applying the migrations again. It should work but if it does not then run makemigrations again and then migrate.
Note: don't forget to take a backup of your data.