如何解析“Django_content_type already exists”?

2024-05-29 02:07:49 发布

您现在位置:Python中文网/ 问答频道 /正文

升级到django 1.8之后,我在迁移期间收到错误:

ProgrammingError: relation "django_content_type" already exists

我对这个错误背后的背景很感兴趣,但更重要的是, 我该怎么解决?


Tags: djangotype错误existscontent背景relationalready
3条回答

我在Ubuntu18.04+Postgres10.10版本的Django2.2.7或Django3.0上解决了这个问题。

  1. Restore the database in Postgres database (used pgAdmin tool for this)
  2. (virtualenv)python manage.py loaddata dumpfile.json
  3. Dropping django_migrations table from database (used pgAdmin tool for this)
  4. (virtualenv)python manage.py makemigrations
  5. (virtualenv)python manage.py migrate --fake
  6. (virtualenv)python manage.py migrate
  7. (virtualenv)python manage.py collectstatic
  8. (virtualenv)python manage.py runserver 0.0.0.0:8000

我授予用户在该特定数据库上的所有权限,它解决了问题。

项目上的初始迁移有时会遇到问题,使用--伪初始

python manage.py migrate --fake-initial

这是1.8的新版本。在1.7中,-fake initial是一个隐式默认值,但在1.8中是显式的。

从文档中:

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.

https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial

相关问题 更多 >

    热门问题