When i run
python manage.py migrate
on my django project, i gets the following error
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/hari/project/env/local/lib/python2.7/site- packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/hari/project/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/hari/project/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/hari/project/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/hari/project/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 86, in handle
executor.loader.check_consistent_history(connection)
File "/home/hari/project/env/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 298, in check_consistent_history
connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'.
I have a user model like below
class User(AbstractUser):
place = models.CharField(max_length=64, null=True, blank=True)
address = models.CharField(max_length=128, null=True, blank=True)
So how can i solve this?
when you create a new Django project and run
The Django will create 10 tables for you by default including one auth_user table and two start with auth_user.
when you want to create a custom user model inherit from AbstractUser, you will encounter this problem with error message as follow:
I solve this problem by dropping my entire database, and create a new one. And this replaced the three tables I mentioned.
If you set AUTH_USER_MODEL in settings.py like this:
you should comment this line before run makemigration and migrate commands. Then you can uncomment this line again.
when you create a new project and with no apps, you run the
the Django will create 10 tables by default.
If you want create a customer user model which inherit from
AbstractUser
after that, you will encounter this problem as follow message:finally, I drop my entire databases and run
Here how to solve this properly.
Follow these steps in your migrations folder inside the project:
python manage.py makemigrations
python manage.py migrate
Voila.
If that exception was reveal itself while you are trying to create your own User model instead of standard follow that instruction
I have found my problem resolve by follow that instruction step by step:
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 is 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 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.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!