Python 如何解决“django_content_type 已存在”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29760817/
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
How can I resolve 'django_content_type already exists'?
提问by Dan O'Boyle
After upgrading to django 1.8 I'm recieving the error during migration:
升级到 django 1.8 后,我在迁移过程中收到错误:
ProgrammingError: relation "django_content_type" already exists
I'd be interested in the background behind this error, but more importantly, How can I resolve it?
我会对这个错误背后的背景感兴趣,但更重要的是,我该如何解决?
采纳答案by Dan O'Boyle
Initial migrations on a project can sometimes be troubleshot using --fake-initial
有时可以使用 --fake-initial 对项目的初始迁移进行故障排除
python manage.py migrate --fake-initial
It's new in 1.8. In 1.7, --fake-initial was an implicit default, but explicit in 1.8.
它是 1.8 中的新功能。在 1.7 中,--fake-initial 是一个隐含的默认值,但在 1.8 中是明确的。
From the Docs:
从文档:
The --fake-initial option can be used to allow Django to skip an app's initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for matching database schema beyond matching table names and so is only safe to use if you are confident that your existing schema matches what is recorded in your initial migration.
--fake-initial 选项可用于允许 Django 跳过应用程序的初始迁移,如果具有该迁移中所有 CreateModel 操作创建的所有模型的名称的所有数据库表已经存在。此选项旨在在首次针对预先存在使用迁移的数据库运行迁移时使用。但是,此选项不会检查匹配表名称之外的匹配数据库架构,因此只有在您确信现有架构与初始迁移中记录的架构匹配时才可以安全使用。
https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial
https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial
回答by adriaanbd
I granted all privileges to the user on that specific database and it solved the issue.
我授予用户对该特定数据库的所有权限,并解决了该问题。
回答by Santana
I solved this issue on Django 2.2.7 or Django 3.0 hosted on Ubuntu 18.04 + Postgres 10.10 version.
我在 Ubuntu 18.04 + Postgres 10.10 版本上托管的 Django 2.2.7 或 Django 3.0 上解决了这个问题。
- Restore the database in Postgres database (used pgAdmin tool for this)
- (virtualenv)python manage.py loaddata dumpfile.json
- Dropping django_migrations table from database (used pgAdmin tool for this)
- (virtualenv)python manage.py makemigrations
- (virtualenv)python manage.py migrate --fake
- (virtualenv)python manage.py migrate
- (virtualenv)python manage.py collectstatic
- (virtualenv)python manage.py runserver 0.0.0.0:8000
- 在 Postgres 数据库中恢复数据库(为此使用了 pgAdmin 工具)
- (virtualenv)python manage.py loaddata dumpfile.json
- 从数据库中删除 django_migrations 表(为此使用 pgAdmin 工具)
- (virtualenv)python manage.py makemigrations
- (virtualenv)python manage.py migrate --fake
- (virtualenv)python manage.py migrate
- (virtualenv)python manage.py collectstatic
- (virtualenv)python manage.py runserver 0.0.0.0:8000