简体   繁体   English

Django Python loaddata失败,并出现django.db.utils.IntegrityError

[英]Django Python loaddata fails with django.db.utils.IntegrityError

Took over a database project and I am struggling to load the remote database into the local database. 完成了一个数据库项目,我正在努力将远程数据库加载到本地数据库中。

The app was built with django and the local database still relies on sqlite that comes with out of the box. 该应用程序是使用django构建的, 本地数据库仍然依赖于现成的sqlite

The remote database is of postgresql type unfortunately and illogically. 不幸的是, 远程数据库属于postgresql类型,而且不合逻辑。

The code I am trying to run in the terminal: 我试图在终端中运行的代码:

python manage.py loaddata *[path to backup.json file]*

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. 我遇到一些完整性错误,因此像任何有理智的人一样,我刷新了本地db,因为自从我想以任何方式加载远程数据。

python manage.py flush python manage.py syncdata

Now when I try to load the data from the json file I get the following error: 现在,当我尝试从json文件加载数据时,出现以下错误:

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 django.db.utils.IntegrityError:安装夹具'C:... \\ lit \\ backups \\ dbbackup_20190915_145546.json'时出现问题:无法加载contenttypes.ContentType(pk = 1):UNIQUE约束失败:django_content_type.app_label,django_conten t_type。模型

Changing the settings.py file from: 从以下位置更改settings.py文件:

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

to

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

just gives me a new error. 只是给我一个新的错误。

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. django.db.utils.IntegrityError:安装夹具'C:.. \\ lit \\ backups \\ dbbackup_20190915_145546.json'时出现问题:无法加载contenttypes.ContentType(pk = 17):重复键值违反了唯一约束“ django_content_type_a pp_label_model_76bd3d3b:uniqTA密钥(app_label,model)=(admin,logentry)已存在。

I already ran 我已经跑了

python manage.py makemigrations
python manage.py migrate

in your local database you create some ContentType instances. 在本地数据库中,您将创建一些ContentType实例。

when you migrate your remote database all ContentType for your models created again. 当您迁移远程数据库时,将再次创建模型的所有ContentType。

but when you want to load data you try to load this instances again. 但是当您要加载数据时,请尝试再次加载该实例。

you have 2 solutions 你有2个解决方案

1- remove all content types instances from remote host using django shell 1-使用Django Shell从远程主机删除所有内容类型实例

python manage.py shell

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

2- remove content type instances from dumped data 2-从转储的数据中删除内容类型实例

python manage.py dumpdata --exclude contenttypes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试失败,出现“ django.db.utils.IntegrityError” - Test fails with `django.db.utils.IntegrityError` django.db.utils.IntegrityError: - django.db.utils.IntegrityError: [Django的] django.db.utils.IntegrityError - [Django]django.db.utils.IntegrityError 自定义UserModel中的django.db.utils.IntegrityError - django.db.utils.IntegrityError in Custom UserModel Django app:由于django.db.utils.IntegrityError,单元测试失败 - Django app : unit tests fails because of django.db.utils.IntegrityError Django makemigrations工作,迁移失败“django.db.utils.IntegrityError:NOT NULL约束失败” - Django makemigrations works, migrate fails with “django.db.utils.IntegrityError: NOT NULL constraint failed” django.db.utils.IntegrityError: FOREIGN KEY 约束在 django 中失败 - django.db.utils.IntegrityError: FOREIGN KEY constraint failed in django django.db.utils.IntegrityError: FOREIGN KEY 约束在通过 Selenium 和 Python Django 执行 LiveServerTestCases 时失败 - django.db.utils.IntegrityError: FOREIGN KEY constraint failed while executing LiveServerTestCases through Selenium and Python Django django.db.utils.IntegrityError: (1452, '无法添加或更新子行:外键约束失败 - django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails django.db.utils.IntegrityError:列中的值为空-但值不为空 - django.db.utils.IntegrityError: null value in column - but value is not null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM