Django Python loaddata失败,并出现django.db.utils.IntegrityError [英] Django Python loaddata fails with django.db.utils.IntegrityError

查看:429
本文介绍了Django Python loaddata失败,并出现django.db.utils.IntegrityError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接管一个数据库项目,我正在努力将远程数据库加载到本地数据库中。

Took over a database project and I am struggling to load the remote database into the local database.

该应用是使用django和本地数据库构建的仍然依赖于开箱即用的 sqlite

The app was built with django and the local database still relies on sqlite that comes with out of the box.

远程数据库 postgresql 类型。

我要在终端中运行的代码:

The code I am trying to run in the terminal:

python manage.py loaddata * [backup.json文件的路径] *

我遇到了一些完整性错误,所以像任何有理智的人一样,我刷新了本地db,因为因为我想无论如何都要加载远程数据。

I get some integrity error so like any reasonable man I flushed the local db because since I want to anyhows load the remote data.

python manage.py flush
python manage.py syncdata

现在,当我尝试从json文件加载数据时,出现以下错误:

Now when I try to load the data from the json file I get the following error:


django.db .utils.IntegrityError:安装夹具'C:...时出现问题:... litlitbackups\dbbackup_20190915_145546.json':无法加载contenttypes.ContentType(pk = 1):UNIQUE约束失败:django_content_type.app_label,django_conten
t_type.model

django.db.utils.IntegrityError: Problem installing fixture 'C:...\lit\backups\dbbackup_20190915_145546.json': Could not load contenttypes.ContentType(pk=1): UNIQUE constraint failed: django_content_type.app_label, django_conten t_type.model

settings.py 文件从以下位置更改:

Changing the settings.py file from:

`DATABASES = {
    'default':  {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}`

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

只是给我一个新错误。


django.db.utils.IntegrityError:安装时出现问题夹具'C:.. \lit\backups\dbbackup_20190915_145546.json':无法加载contenttypes.ContentType(pk = 17):重复键值违反了唯一约束 django_content_type_a
pp_label_model_76bd3d3b_uniq
详细信息:密钥(app_label,model)=(admin,logentry)已经存在。

django.db.utils.IntegrityError: Problem installing fixture 'C:..\lit\backups\dbbackup_20190915_145546.json': Could not load contenttypes.ContentType(pk=17): duplicate key value violates unique constraint "django_content_type_a pp_label_model_76bd3d3b_uniq" DETAIL: Key (app_label, model)=(admin, logentry) already exists.

我已经运行了

python manage.py makemigrations
python manage.py migrate


推荐答案

在本地数据库中创建一些ContentType实例。

in your local database you create some ContentType instances.

在迁移远程数据库时

,但是当您要加载数据时,尝试再次加载该实例。

but when you want to load data you try to load this instances again.

你有2个解决方案

1-使用Django Shell从远程主机删除所有内容类型实例

1- remove all content types instances from remote host using django shell

python manage.py shell

>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()

2-从转储数据中删除内容类型实例

2- remove content type instances from dumped data

python manage.py dumpdata --exclude contenttypes

这篇关于Django Python loaddata失败,并出现django.db.utils.IntegrityError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆