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
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.
python3 -m manage makemigrations
. This should remove any problems you had with dependencies or inconsistencies in your migrations. InconsistentMigrationHistory
is complaining about].python3 -m manage migrate
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.
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.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 the django_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 the django_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. 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!
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.
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.
I encountered this when migrating from Wagtail 2.0 to 2.4, but have seen it a few other times when a third party app squashes a migration after your current version but before the version you’re migrating to.
The shockingly simple solution in this case at least is:
./manage.py migrate
./manage.py makemigrations
./manage.py migrate
i.e. run a single migrate before trying to makemigrations.
delete migrations folder and db.sqlite3 and type in the cmd python manage.py makemigrations
Your Error is essentially:
Migration "B" is applied before its dependency "A" on database 'default'.
Sanity Check: First, open your database and look at the records in the 'django_migrations' table. Records should be listed in Chronological order (ex: A,B,C,D...).
Make sure that the name of the "A" Migration listed in the error matches the name of the "A" migration listed in the database. (They can differ if you had previously, manually, edited or deleted or renamed migration files)
To Fix This, rename migration A. either in the database or rename the filename. BUT make sure the changes matches up with what other developers on your team have in their databases (or the changes matches what on your production database)
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'