Python django.db.migrations.exceptions.InconsistentMigrationHistory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44651760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:19:09  来源:igfitidea点击:

django.db.migrations.exceptions.InconsistentMigrationHistory

pythondjangodjango-modelsdjango-rest-frameworkdatabase-migration

提问by Hari Krishnan

When I run python manage.py migrateon my Django project, I get the following error:

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

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)

How can I solve this problem?

我怎么解决这个问题?

采纳答案by Arpit Solanki

Your django_migrations table in your database is the cause of inconsistency and deleting all the migrations just from local path won't work.

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

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.

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

Note: don't forget to take a backup of your data.

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

回答by Hymanson

Since you are using a custom User model, you can first comment out

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

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

in your Installed_Apps settings. Then run

在您的 Installed_Apps 设置中。然后运行

python manage.py migrate.

When done uncomment

完成后取消注释

'django.contrib.admin'

回答by Airs

Lets start off by addressing the issue with most of the answers on this page:

让我们从解决此页面上大部分答案的问题开始:

You never haveto drop your database if you are using Django's migration system correctly and you shouldnever delete migrations once they are comitted

如果您正确使用 Django 的迁移系统,您永远不必删除您的数据库,并且一旦它们被提交,您就不应该删除迁移

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.

现在,最适合您的解决方案取决于许多因素,包括您对 Django 的经验、您对迁移系统的理解程度以及数据库中数据的价值。

In short there are two ways you can address any migration error.

简而言之,有两种方法可以解决任何迁移错误。

  1. Take the nuclearoption. Warning:this is only an option if you are working alone. If other people depend on existing migrations you cannotjust 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 InconsistentMigrationHistoryis 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 youdid. (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 --mergecan 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 neverdelete 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 InconsistentMigrationHistoryissue 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_migrationstable or deleted a migration afterit 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_migrationstable 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.
  1. 采取选项。警告:这只是您独自工作时的一种选择。如果其他人依赖于现有的迁移,您不能只是删除它们。

    • 删除所有迁移,并使用python3 -m manage makemigrations. 这应该会消除您在迁移中遇到的依赖项或不一致问题。
    • 删除整个数据库。这将消除您在实际数据库架构与基于迁移历史应该具有的架构之间存在的任何不一致问题,并将消除您在迁移历史记录与以前的迁移文件之间存在的不一致问题[这就是的InconsistentMigrationHistory抱怨。
    • 重新创建您的数据库架构 python3 -m manage migrate
  2. 确定错误的原因并解决它,因为(根据经验)原因几乎可以肯定是所做的一些愚蠢的事情。(一般是因为不了解如何正确使用迁移系统)。根据我造成的错误分为三类。

    1. 与迁移文件不一致。当多人在一个项目上工作时,这是一种很常见的情况。希望您的更改不会发生冲突并且makemigrations --merge可以解决这个问题,否则有人将不得不将他们的迁移回滚到分支点才能解决这个问题。
    2. 您的架构和迁移历史记录之间存在不一致。为了管理这个,有人要么手动编辑数据库架构,要么删除迁移。如果他们删除了迁移,则恢复他们的更改并对他们大喊大叫;你应该永远不会删除迁移,如果别人依赖他们。如果他们手动编辑了数据库架构,请还原他们的更改,然后对他们大喊大叫;Django 正在管理数据库架构,没有其他人。
    3. 您的迁移历史记录和迁移文件之间存在不一致。[这是InconsistentMigrationHistory提问者遇到的问题,也是我到达此页面时遇到的问题]。为了管理这个,有人要么手动弄乱了django_migrations表,要么在应用删除了迁移。要解决这个问题,您将不得不弄清楚不一致是如何产生的并手动解决它。如果您的数据库架构正确,并且只是您的迁移历史有误,您可以手动编辑django_migrations表来解决此问题。如果您的数据库架构错误,那么您还必须手动编辑它以使其符合应有的状态。

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.

根据您对问题的描述和您选择的答案,我将假设您是单独工作,是 Django 新手,并且不关心您的数据。因此,核选项可能适合您。

If you are not in this situation and the above text looks like gibberish, then I suggest asking the Django User's Mailing Listfor help. There are very helpful people there who can help walk you through resolving the specific mess you are in.

如果你不是这种情况并且上面的文字看起来像胡言乱语,那么我建议向Django 用户邮件列表寻求帮助。那里有非常乐于助人的人,他们可以帮助您解决具体的麻烦。

Have faith, you can resolve this error without going nuclear!

有信心,您可以在不去核的情况下解决此错误!

回答by Dr. Younes Henni

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
  1. 删除 _pycache_ 和 0001_initial 文件。
  2. 从根目录中删除 db.sqlite3(小心你的所有数据都会消失)。
  3. 在终端运行:
      python manage.py makemigrations
      python manage.py migrate

Voila.

瞧。

回答by user9414732

This happened to me in a new project after I added a custom User model, per the recommendation in the django docs.

根据 django 文档中的建议,在我添加自定义用户模型后,这发生在我的新项目中。

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.

如果您正在开始一个新项目,强烈建议设置自定义用户模型,即使默认的 User 模型对您来说已经足够了。

Here is what I did to solve the problem.

这是我为解决问题所做的工作。

  1. Delete the database db.sqlite3.
  2. Delete the app/migrations folder.
  1. 删除数据库db.sqlite3。
  2. 删除 app/migrations 文件夹。

Per @Hymanson, temporarily comment out django.contrib.admin.

根据@Hymanson,暂时注释掉 django.contrib.admin。

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

Also comment out the admin site in urls.py:

还要注释掉 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

如果你不注释掉路径('admin/'),你会在运行时得到错误“LookupError: No installed app with label 'admin'”

python manage.py migrate

After the migrations finish, uncomment both of the above.

迁移完成后,取消对上述两项的注释。

回答by kun shi

Problem

问题

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

django.db.migrations.exceptions.InconsistentMigrationHistory:迁移 admin.0001_initial 在其依赖 account.0001_initial 之前应用于数据库“default”。

So we can migrate database without admin(admin.0001_initial) firstly.

所以我们可以先在没有admin(admin.0001_initial)的情况下迁移数据库。

After its dependency migrated, execute commands to migrate admin.0001_initial.

其依赖迁移后,执行命令迁移admin.0001_initial

Solution

解决方案

  1. remove 'django.contrib.admin' from INSTALLED_APPS in settings.py.
  2. execute commands:
  1. 从 settings.py 中的 INSTALLED_APPS 中删除“django.contrib.admin”。
  2. 执行命令:

Python manage.py makemigrations appname

Python manage.py migrate appname

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:
  1. 将 'django.contrib.admin' 添加到 settings.py 文件中的 INSTALLED_APPS。
  2. 再次执行命令:

$: Python manage.py makemigrations appname

$: Python manage.py migrate appname

$: Python manage.py makemigrations appname

$: Python manage.py migrate appname

回答by hcf1425

when you create a new Django project and run

当您创建一个新的 Django 项目并运行时

python manage.py migrate

python manage.py 迁移

The Django will create 10 tables for you by default including one auth_user table and two start with auth_user.

Django 会默认为你创建 10 个表,包括一个 auth_user 表和两个以 auth_user 开头的表。

when you want to create a custom user model inherit from AbstractUser, you will encounter this problem with error message as follow:

当你想创建一个从 AbstractUser 继承的自定义用户模型时,你会遇到这个问题,错误消息如下:

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.

我通过删除整个数据库并创建一个新数据库来解决这个问题。这取代了我提到的三个表。

回答by Arun

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

只需删除 sqlite 文件或运行刷新数据库“python manage.py flush”,然后分别运行 makemigrations 和 migrate 命令。

回答by Duilio

If you are working on an empty database a quick fix could be running the migrations for the accountapp, before any other app migrations.

如果您正在处理一个空数据库,一个快速修复可能是在任何其他应用程序迁移之前运行帐户应用程序的迁移。

$ ./manage.py migrate account

And then:

进而:

$ ./manage.py migrate

回答by NaSir HuSSaiN

Solved by commenting this before migration in settings.py enter code here'django.contrib.admin' and in urls.py, enter code herepath('admin/', admin.site.urls). uncomment after migrate

通过在 settings.py enter code here'django.contrib.admin' 和 urls.py, enter code herepath('admin/', admin.site.urls) 中迁移之前对此进行评论来解决 。迁移后取消注释