问题描述
我目前正在使用sqlite3作为我的一个Django项目的DB。我想改变这个来使用postgresql,我想保持所有的数据不变。
I am currently using sqlite3 as the DB for one of my Django projects. I want to change this to use postgresql, and I would like to keep all the data intact.
我使用了 ./ manage.py dumpdata> ; dump.json
创建数据转储,并将我的设置更改为使用postgresql。尝试使用空数据库来执行 ./ manage.py loaddata dump.json
导致关于表不存在的错误,因此我运行 ./管理.py syncdb
,并再次尝试。这导致了这个错误:
I used ./manage.py dumpdata > dump.json
to create a dump of the data, and changed my settings to use postgresql. Trying first with an empty database to do ./manage.py loaddata dump.json
resulted in errors about tables not existing, so I ran ./manage.py syncdb
, and tried again. That results in this error:
Problem installing fixture 'dump.json': Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 163, in handle
obj.save()
File "/usr/lib/python2.6/site-packages/django/core/serializers/base.py", line 163, in save
models.Model.save_base(self.object, raw=True)
File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 495, in save_base
rows = manager.filter(pk=pk_val)._update(values)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 448, in _update
return query.execute_sql(None)
File "/usr/lib/python2.6/site-packages/django/db/models/sql/subqueries.py", line 124, in execute_sql
cursor = super(UpdateQuery, self).execute_sql(result_type)
File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 2347, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 19, in execute
return self.cursor.execute(sql, params)
IntegrityError: duplicate key value violates unique constraint "django_content_type_app_label_key"
- 这不是将数据从一个数据库移动到另一个数据库的正确方法?
- 为了安全切换DB后端,我该怎么做? li>
- Is this not the correct way to move data from one database to another?
- What should I be doing in order to switch DB backend safely?
推荐答案
问题只在于你得到的内容类型定义了两次 - 一旦你做
syncdb
,然后从导出的数据中导入一次。由于您的数据库中可能还有其他项目依赖于原始内容类型定义,所以建议您保留这些内容。The problem is simply that you're getting the content types defined twice - once when you do
syncdb
, and once from the exported data you're trying to import. Since you may well have other items in your database that depend on the original content type definitions, I would recommend keeping those.所以运行
syncdb
,domanage.py dbshell
并在您的数据库中执行TRUNCATE django_content_type;
以删除所有新定义的内容类型。那么你不应该有任何冲突 - 在这个过程中,无论如何。So, after running
syncdb
, domanage.py dbshell
and in your database doTRUNCATE django_content_type;
to remove all the newly-defined content types. Then you shouldn't get any conflicts - on that part of the process, in any case.这篇关于将Django项目从sqlite3后端切换到postgresql时加载datadump失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!