django.db.migrations.exceptions.InconsistentMigrat

2019-01-16 17:38发布

问题:

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?

回答1:

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.



回答2:

Since you are using a custom User model, your 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’.


回答3:

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.

  1. 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.

    • 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
  2. 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.

    1. 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.
    2. 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.
    3. 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 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!



回答4:

Here how to solve this properly.

Follow these steps in your migrations folder inside the project:

  1. Delete the _pycache_ and the 0001_initial files.
  2. Delete the db.sqlite3 from the root directory (be careful all your data will go away).
  3. on the terminal run:
      python manage.py makemigrations
      python manage.py migrate

Voila.



回答5:

Problem

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.

Solution

  1. remove 'django.contrib.admin' from INSTALLED_APPS in settings.py.
  2. execute commands:

Python manage.py makemigrations appname

Python manage.py migrate appname

  1. add 'django.contrib.admin' to INSTALLED_APPS in settings.py file.
  2. execute commands again:

$: Python manage.py makemigrations appname

$: Python manage.py migrate appname



回答6:

just delete the sqlite file or run flush the databse 'python manage.py flush' and then run makemigrations and migrate commands respectively.



回答7:

First delete all the migrations and db.sqlite3 files and follow these steps:

$ ./manage.py makemigrations myapp 
$ ./manage.py squashmigrations myapp 0001(may be differ)

Delete the old migration file and finally.

$ ./manage.py migrate


回答8:

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:

  1. Create a custom user model identical to auth.User, call it User (so many-to-many tables keep the same name) and set db_table='auth_user' (so it uses the same table)
  2. Throw away all your migrations
  3. Recreate a fresh set of migrations
  4. Sacrifice a chicken, perhaps two if you're anxious; also make a backup of your database
  5. Truncate the django_migrations table
  6. Fake-apply the new set of migrations
  7. Unset db_table, make other changes to the custom model, generate migrations, apply them

It is highly recommended to do this on a database that enforces foreign key constraints. Don't try this on SQLite on your laptop and expect it to work on Postgres on the servers!



回答9:

If you set AUTH_USER_MODEL in settings.py like this:

AUTH_USER_MODEL = 'custom_user_app_name.User'

you should comment this line before run makemigration and migrate commands. Then you can uncomment this line again.



回答10:

when you create a new project and with no apps, you run the

python manage.py migrate

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:

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'.

finally, I drop my entire databases and run



回答11:

when you create a new Django project and run

python manage.py migrate

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:

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'.

I solve this problem by dropping my entire database, and create a new one. And this replaced the three tables I mentioned.