我生成了一个fixture:
python manage.py dumpdata --all > ./mydump.json我清空了我所有的数据库,使用:
python manage.py sqlflush | psql mydatabase -U mydbuser但是当我尝试使用loaddata时:
python manage.py loaddata ./mydump.json我收到这个错误:
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.我在生产上遇到了这个问题,我已经没有想法了。有人也有类似的问题吗?
发布于 2014-04-04 12:12:08
Jeff Sheffield的解决方案是正确的,但是现在我发现像django-dbbackup这样的解决方案是迄今为止对任何数据库都是最通用和最简单的方法。
python manage.py dbbackup发布于 2020-02-29 04:50:35
使用注释掉的所有@reciever运行loaddata,因为它们将在loaddata加载数据时触发。如果@reciever%s创建其他对象作为副作用,它将导致冲突。
发布于 2014-02-10 14:51:20
unix First:我相信你的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.django Edited:(非常重要)确保您的服务器上没有任何活动的连接。然后:
$ 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.jsonSecond:如果您的管道正常,则为。有可能是这样。根据您的模式/数据,您可能需要使用自然键。
# 1: Dump your json using ( -n ) natural keys.
$ python manage.py dumpdata -n --all > ./mydump.json
# followed by steps 2-5 above.https://stackoverflow.com/questions/21654559
复制相似问题