django.db.migrations.exceptions.InconsistentMigrat

2019-01-16 17:10发布

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?

11条回答
太酷不给撩
2楼-- · 2019-01-16 17:47

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
查看更多
姐就是有狂的资本
3楼-- · 2019-01-16 17:53

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.

查看更多
叼着烟拽天下
4楼-- · 2019-01-16 17:53

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

查看更多
男人必须洒脱
5楼-- · 2019-01-16 17:55

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’.
查看更多
等我变得足够好
6楼-- · 2019-01-16 17:55

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

查看更多
登录 后发表回答