0

Im trying to do the below command but im getting this long error. I have also tried to do go into the dbshell to trucanate as i saw suggested elsewhere but im getting the error that psql is not install or path found even though it should be.

I have been succesful in managing to get my tables in the postgres database but their are all empty except a few, which i find strange. My Json dump file has everything it needs in it but it wont transfer over.

Anyone have any ideas?

(venv) DEMOPROJECT>python manage.py loaddata "datadump.json" Traceback (most recent call last): File "\PycharmProjects\WebP1\venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params) psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "django_content_type_app_label_model_76bd3d3b_uniq" DETAIL: Key (app_label, model)=(admin, logentry) already exists. django.db.utils.IntegrityError: Problem installing fixture '\DEMOPROJECT\datadump.json': Could not load contenttypes.ContentType(pk=1): duplicate key value violates unique constraint "django_content_type_app_label_model_76bd3 d3b_uniq" DETAIL: Key (app_label, model)=(admin, logentry) already exists. 

2 Answers 2

2

ContentType is a sort of registry of an app's models, intended as an interface to be able to access information about a model without having to know a whole lot about the model specifics.
The django auth framework uses ContentType to map permissions to models. The django admin app tracks changes to its objects via the model LogEntry which itself uses ContentTypes (that's the model that is causing the error you've posted).

If ContentType is queried for a model that it doesn't know already, a new record with that model is created: that means that the makeup of the ContentType table can differ from environment to environment - it depends on the order in which models are requested in.

Seeing that your dump already contains data for ContentType, and assuming that for any model that uses ContentType, the dump also contains the references to that new ContentType table, you should be fine deleting all entries that are currently in the ContentType table of your local, outdated database:

from django.contrib.contenttypes.models import ContentType ContentType.objects.all().delete() 

You should then be able to load your datadump without a problem.

You can make a backup of the contents of your ContentType, if you want to make sure nothing gets lost:

python manage.py dumpdata contenttypes.ContentType 
Sign up to request clarification or add additional context in comments.

Comments

0

Notice that your unique index is getting duplicate: Key (app_label, model)=(admin, logentry) already exists.

That would mean one of following:

  • your json dump file contains incorrect duplicate entries
  • you already have database loaded or partially loaded

1 Comment

The json file should be fine as i was following a video tutorial. My database works fine on sqlite but i want to move it to postresql as Ive heard its far better. It was after the migrate --sync-db and loaddata commands I got my error. The migrate seemed to load in some wagtail data but didnt load in any of my django models from the same json dump. Im presuming the loaddata command is meant to insert the data with the created tables Cant work it out for the life of me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.