Solving Django Python Error – django.db.migrations.exceptions.InconsistentMigrationHistory

django.db.migrations.exceptions.InconsistentMigrationHistory is a Python WebDriver error which occurs when you try to make a migration in Django

In this article I am going to try and solve the error while I try to explain why the error is actually popping up, we will also introduce some solutions that worked for other developers and see if those solutions can solve the error in your particular situation.

Explaining the Error : django.db.migrations.exceptions.InconsistentMigrationHistory

The problem happens when we use Djano and try to make a migration

The error message should look like the error in the example bellow. Double check in order to avoid any confusion

                                                                       #
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
....
connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is 
applied before its dependency account.0001_initial on database 'default'.
                                                                       #

Bellow are the solutions which worked for me and will help you to successfully solve the issue

Solution 1 : step by step solution

The easiest solution to the issue is to follow these instructions step by step.

First, you should start by deleting the database db.sqlite3.

The second step is to Delete the migrations/app folder/directory except init.py

Third step is to search for django.contrib.admin and turn it into a comment by adding #

                                                                       #
INSTALLED_APPS = [
.......................
#‘django.contrib.admin’,
.......................
]
                                                                       #

Forth step is to search urls.py for the admin site and turn it into a comment by adding #

                                                                       #
urlpatterns = [
    path('profile/', include('restapp.urls')),
    #path('admin/', admin.site.urls),
]
                                                                       #

Do not uncomment them until you run and the migration succeeds

                                                                       #
python manage.py migrate
                                                                       #

Now, you can uncomment. If the solution is not enough check out the fix bellow

Solution 2 : Another step by step solution

The second solution is also a step by step solution that may work for you

The first step is to remove django.contrib.admin from INSTALLED_APPS in settings.py.

Then run the command bellow

                                                                       #
Python manage.py makemigrations appname
                                                                       #

Followed by the command bellow

                                                                       #
Python manage.py migrate appname
                                                                       #

Now, you can add django.contrib.admin back to INSTALLED_APPS in settings.py file.

Again, run the command bellow

                                                                       #
Python manage.py makemigrations appname
                                                                       #

And finnaly, you should run this last command

                                                                       #
Python manage.py migrate appname
                                                                       #

These commands should be enough to get rid of the error for good.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

Here we are at the end of the road, at the end of this article, if you solved this error congrats, this was a confusing error for me the first time I encountered it. Make sure to keep coding and keep learning, Python is my favourite programming language, it just needs some patience, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/