【问题标题】:production issue: sqlite3 to postgresql migration issue in django生产问题:django 中的 sqlite3 到 postgresql 迁移问题
【发布时间】:2021-01-04 15:27:46
【问题描述】:

我正在尝试将我的 django 项目从 sqlite3 迁移到 postgresql。我在开发中成功完成了此操作,但在生产中出现错误。

我已使用此命令创建转储:

manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

然后我在我的 settings.py 中更改了数据库

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'dbname',
    'USER': 'username',
    'PASSWORD': 'password',
    'HOST': 'localhost',
    'PORT': '5432',
}

然后我排除内容类型如下:

python3 manage.py shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
quit()

但我收到此错误:

django.db.utils.IntegrityError: Problem installing fixtures: insert or update on table 
"django_admin_log" violates foreign key constraint 
"django_admin_log_content_type_id_c4bce8eb_fk_django_co"
DETAIL:  Key (content_type_id)=(7) is not present in table "django_content_type".

请帮忙

【问题讨论】:

  • 你删除所有ContentTypes...所以GenericForeignKeys 将不再工作,因为表是空的,因此你不能再引用ContentType...
  • 我尝试使用ContentType.objects.all().delete() 删除,但仍然报错,还有其他方法吗?
  • 当然,为什么要删除ContentTypes?
  • @barkatpathan 尝试运行此ContentType.objects.clear_cache()
  • answer 中,他建议删除它们,但错误仍然存​​在,只是密钥从 20 更改为 7

标签: django postgresql production


【解决方案1】:

好的,我想通并成功迁移。 感谢ankitWillem Van Onsem Mostafa Ghadimi

我一步一步跟着穆斯塔法·加迪米的回答here

也清除了ankit建议的缓存

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 2017-02-14
    • 2013-09-14
    • 2015-08-30
    • 1970-01-01
    • 2021-11-20
    • 2011-10-26
    相关资源
    最近更新 更多