Django: loaddata not working

I generated the device:

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 get 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 have a production problem and I have no ideas. Has anyone had a similar problem?

+4
source share
2 answers

Jeff Sheffield's solution is correct, but now I have found that a solution like django-dbbackup is by far the most general and simpler way it is with any database.

python manage.py dbbackup
+2
source

First: I believe your unix channel is spelled incorrectly.

# 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. :

$ 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

: .. . / .

# 1: Dump your json using ( -n ) natural keys.
$ python manage.py dumpdata -n --all > ./mydump.json

# followed by steps 2-5 above.
+5

Source: https://habr.com/ru/post/1525991/


All Articles