Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate key issue when loading back.json file PostgreSQL

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 ?

like image 270
TJB Avatar asked Sep 26 '17 02:09

TJB


1 Answers

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
like image 96
ARKhan Avatar answered Sep 20 '22 23:09

ARKhan