一尘不染

django.db.migrations.exceptions.InconsistentMigrationHistory

django

python manage.py migrate

在我的django项目上,出现以下错误

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

我有一个如下的用户模型

class User(AbstractUser):
place = models.CharField(max_length=64, null=True, blank=True)
address = models.CharField(max_length=128, null=True, blank=True)

那么我该如何解决呢?


阅读 539

收藏
2020-04-04

共2个答案

一尘不染

数据库中的django_migrations表是导致不一致的原因,仅从本地路径删除所有迁移将不起作用。

您必须从数据库中截断django_migrations表,然后尝试再次应用迁移。它应该可以工作,但是如果不起作用,则再次运行makemigrations,然后进行迁移。

注意:不要忘记备份数据。

2020-04-04
一尘不染

由于你使用的是自定义用户模型,因此你可以先注释掉

INSTALLED_APPS = [
...
#'django.contrib.admin',
...
]

在你的Installed_Apps设置中。然后跑

python manage.py migrate.

取消注释后

'django.contrib.admin'
2020-04-04