[英]Django: loaddata not working
I generated a fixture:我生成了一个夹具:
python manage.py dumpdata --all > ./mydump.json
I emptied all my databases using:我使用以下方法清空了所有数据库:
python manage.py sqlflush | psql mydatabase -U mydbuser
But when i try to use loaddata:但是当我尝试使用 loaddata 时:
python manage.py loaddata ./mydump.json
I'm recieving this error:我收到此错误:
IntegrityError: Could not load tastypie.ApiKey(pk=1): duplicate key
value violates unique constraint "tastypie_apikey_user_id_key"
DETAIL: Key (user_id)=(2) already exists.
I'm having this problem on production and i'm out of ideas.我在生产中遇到了这个问题,我没有想法。 Someone had a similar problem?有人遇到过类似的问题吗?
First: I believe your unix pipe is incorrectly written.第一:我相信你的 unix 管道写错了。
# 1: Dump your json
$ python manage.py dumpdata --all > ./mydump.json
# 2: dump your schema
$ python manage.py sqlflush > schema.sql
# 3: launch psql
# this is how I launch psql ( seems to be more portable between rhel/ubuntu )
# you might use a bit different technique, and that is ok.
Edited: (very important) Make sure you do not have any active django connections running on your server.编辑:(非常重要)确保您的服务器上没有运行任何活动的 django 连接。 Then:然后:
$ sudo -u myuser psql mydatabase
# 4: read in schema
mydatabase=# \i schema.sql
mydatabase=# ctrl-d
# 5: load back in your fixture.
$ python manage.py loaddata ./mydump.json
Second: If your pipe is ok.. and it might be.第二:如果你的管子没问题......它可能是。 Depending on your schema/data you may need to use natural-keys.根据您的架构/数据,您可能需要使用自然键。
# 1: Dump your json using ( -n ) natural keys.
$ python manage.py dumpdata -n --all > ./mydump.json
# followed by steps 2-5 above.
Run loaddata
with all @reciever
s commented out because they will be fired when loaddata
loads your data.在注释掉所有@reciever
的情况下运行loaddata
,因为当loaddata
加载您的数据时它们将被触发。 If @reciever
s create other objects as a sideeffect it will cause collisions.如果@reciever
s 创建其他对象作为副作用,它将导致冲突。
Jeff Sheffield 的解决方案是正确的,但现在我发现像django-dbbackup这样的解决方案是迄今为止对任何数据库执行此操作的最通用和最简单的方法。
python manage.py dbbackup
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.