【发布时间】: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