Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 624
Adrian Lopez Avatar asked Feb 09 '14 02:02

Adrian Lopez


3 Answers

Run loaddata with all @recievers commented out because they will be fired when loaddata loads your data. If @recievers create other objects as a sideeffect it will cause collisions.

like image 127
Jan Kaifer Avatar answered Nov 15 '22 01:11

Jan Kaifer


First: I believe your unix pipe is incorrectly written.

# 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. 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.
like image 20
Jeff Sheffield Avatar answered Nov 15 '22 02:11

Jeff Sheffield


Jeff Sheffield's solution is correct, but now I find that a solution like django-dbbackup is by far the most generic and simplier way to do it with any database.

python manage.py dbbackup
like image 3
Adrian Lopez Avatar answered Nov 15 '22 00:11

Adrian Lopez