15

I have a PostgreSQL database that where I performed python manage.py dumpdata to backup the data into a json file. I created a new PostgreSQL database, performed a migrate, and everything worked like clockwork. When I tried to load the backup.json file with python manage.py loaddata backup.json it, gives me this error.

Could not load contenttypes.ContentType(pk=15): duplicate key value violates unique constraint "django_content_type_app_label_76bd3d3b_uniq"
DETAIL:  Key (app_label, model)=(navigation, navigation) already exists.

I checked phpPgAdmin, and there is a row for News. Is there a way to load the backup json file without including the content types, or better yet dump everything except for content types data ?

2
  • 1
    if you just exclude ContentTypes - you might hit another duplicate and another, and another. It needs wider approach change I think Commented Sep 26, 2017 at 8:03
  • There are some differences between a postgres native pg_restore and django loaddata. This article clearly explains these differences and possible solutions - deephacks.com/articles/entry/… But as @Vao Tsun advices, it would be better to find these constraint issues deep into your data. Commented Sep 26, 2017 at 11:14

2 Answers 2

33

I was getting similar error:

django.db.utils.IntegrityError: Problem installing fixture '/home/knysys/ogmius/ogmius/db.json': Could not load contenttypes.ContentType(pk=2): duplicate key value violates unique constraint "django_content_type_app_label_model_76bd3d3b_uniq" DETAIL: Key (app_label, model)=(auth, user) already exists

After that, I found out that If you are restoring a fresh database from another database, You need to dumpdata like this:

./manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

and then load fresh database like this:

./manage.py loaddata db.json
Sign up to request clarification or add additional context in comments.

2 Comments

this works but now I can not log in
@sutan it seems that the loaddata command has overwrite your users table also. Maybe you have a user from where you have loaded your db.json.
13

You can use the following command to load data:

python manage.py loaddata --exclude auth.permission --exclude contenttypes back.json

1 Comment

Cheers. Just to add, to prevent your user from being overwritten also add --exclude auth.user. And i also needed to add --exclude admin.logentry as i was getting an error relating to content_type ids from uninstalled apps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.