django.db.migrations.exceptions.InconsistentMigrationHistory
Since you are using a custom User model, you can first comment out
INSTALLED_APPS = [
...
#'django.contrib.admin',
...
]
in your Installed_Apps settings. Then run
python manage.py migrate.
When done uncomment
'django.contrib.admin'
Lets start off by addressing the issue with most of the answers on this page:
You never have to drop your database if you are using Django's migration system correctly and you should never delete migrations once they are comitted
Now the best solution for you depends on a number of factors which include how experienced you are with Django, what level of understanding you have of the migration system, and how valuable the data in your database is.
In short there are two ways you can address any migration error.
Take the nuclear option. Warning: this is only an option if you are working alone. If other people depend on existing migrations you cannot just delete them.
- Delete all of your migrations, and rebuild a fresh set with
python3 -m manage makemigrations
. This should remove any problems you had with dependencies or inconsistencies in your migrations. - Drop your entire database. This will remove any problems you had with inconsistencies you had between your actual database schema and the schema you should have based on your migration history, and will remove any problems you had with inconsistencies between your migration history and your previous migration files [this is what the
InconsistentMigrationHistory
is complaining about]. - Recreate your database schema with
python3 -m manage migrate
- Delete all of your migrations, and rebuild a fresh set with
Determine the cause of the error and resolve it, because (speaking from experience) the cause is almost certainly something silly you did. (Generally as a result of not understanding how to use the migration system correctly). Based on the error's I've caused there are three categories.
- Inconsistencies with migration files. This is a pretty common one when multiple people are working on a project. Hopefully your changes do not conflict and
makemigrations --merge
can solve this one, otherwise someone is going to have to roll back their migrations to the branching point in order to resolve this. - Inconsistencies between your schema and your migration history. To manage this someone will have either edited the database schema manually, or deleted migrations. If they deleted a migration, then revert their changes and yell at them; you should never delete migrations if others depend on them. If they edited the database schema manually, revert their changes and then yell at them; Django is managing the database schema, no one else.
- Inconsistencies between your migration history and your migrations files. [This is the
InconsistentMigrationHistory
issue the asker suffers from, and the one I suffered from when I arrived at this page]. To manage this someone has either manually messed with thedjango_migrations
table or deleted a migration after it was applied. To resolve this you are going to have to work out how the inconsistency came about and manually resolve it. If your database schema is correct, and it is just your migration history that is wrong you can manually edit thedjango_migrations
table to resolve this. If your database schema is wrong then you will also have to manually edit that to bring it in line with what it should be.
- Inconsistencies with migration files. This is a pretty common one when multiple people are working on a project. Hopefully your changes do not conflict and
Based on your description of the problem and the answer you selected I'm going to assume you are working alone, are new to Django, and don't care about your data. So the nuclear option may be right for you.
If you are not in this situation and the above text looks like gibberish, then I suggest asking the Django User's Mailing List for help. There are very helpful people there who can help walk you through resolving the specific mess you are in.
Have faith, you can resolve this error without going nuclear!
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.
This happened to me in a new project after I added a custom User model, per the recommendation in the django docs.
If you’re starting a new project, it’s highly recommended to set up a custom user model, even if the default User model is sufficient for you.
Here is what I did to solve the problem.
- Delete the database db.sqlite3.
- Delete the app/migrations folder.
Per @jackson, temporarily comment out django.contrib.admin.
INSTALLED_APPS = [
...
#‘django.contrib.admin’,
...
]
Also comment out the admin site in urls.py:
urlpatterns = [
path('profile/', include('restapp.urls')),
#path('admin/', admin.site.urls),
]
If you don't comment out the path('admin/'), you will get error "LookupError: No installed app with label 'admin'" when you run
python manage.py migrate
After the migrations finish, uncomment both of the above.