当我在Django项目上运行python manage.py migrate时,我会得到以下错误:
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)我该如何解决这个问题?
发布于 2017-06-20 11:31:43
数据库中的django_migrations表是造成不一致的原因,只从本地路径删除所有迁移将无法工作。
您必须从数据库中截断django_migrations表,然后再次尝试应用迁移。它应该能工作,但是如果它不工作的话,它会再次迁移到国外,然后迁移。
注意:不要忘记对数据进行备份。
发布于 2018-01-27 13:12:41
由于您使用的是自定义用户模型,因此可以执行以下四个步骤:
INSTALLED_APPS =.#django.contrib.admin‘,
urlpatterns =. #path('admin/',admin.site.urls) .
python manage.py迁移
发布于 2018-04-19 01:04:59
让我们首先用本页上的大多数答案来解决这个问题:
如果您正确地使用了Django的迁移系统,则‘s有来删除您的数据库,而应该不再删除组合的的迁移操作。
现在,对您来说最好的解决方案取决于许多因素,包括您对Django的经验,您对迁移系统的理解程度,以及数据库中数据的价值。
简而言之,有两种方法可以解决任何迁移错误。
- 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`
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.
根据您对问题的描述和您选择的答案,我将假设您是单独工作的,对于Django来说是新手,不关心您的数据。所以核选择对你来说是对的。
如果您不在这种情况下,并且上面的文本看起来像是胡言乱语,那么我建议向Django用户邮件列表寻求帮助。那里有很多乐于助人的人,他们可以帮助你解决你所处的困境。
有信心,你可以解决这个错误而不去核武器!
https://stackoverflow.com/questions/44651760
复制相似问题