1

I upload my first Django-project into DigitalOcean. After command python manage.py loaddata initial_data.json, I have received this message:

django.db.utils.IntegrityError: Problem installing fixture '/webapps/django_shop/shop/initial_data.json': Could not load contenttypes.ContentType(pk=3): duplicate key value violates unique constraint "django_content_type_app_label_76bd3d3b_uniq" DETAIL: Key (app_label, model)=(auth, permission) already exists.

How can I fix it?

M.javid
  • 6,387
  • 3
  • 41
  • 56
Sergey
  • 23
  • 1
  • 6

2 Answers2

5

I had the same problem and I solved this way

DB with data to export from

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

New DB to import to

python manage.py flush

// Important! Disable all signals on models pre_save and post_save

python manage.py loaddata db.json

// Do not forget to enable all signals that you disabled

Majali
  • 480
  • 7
  • 11
  • 3
    Commenting out "pre_save" and "post_save" solved my issue. – Claudio Oliva Nov 16 '21 at 03:36
  • Is there a way to silence specific signals and/or add it as parameter to the `loaddata` command? I'd like to automate the process without changing the models. update: seems like its possible using the passed "raw" arg: https://stackoverflow.com/questions/3499791/how-do-i-prevent-fixtures-from-conflicting-with-django-post-save-signal-code – Lukr Aug 30 '22 at 10:26
  • 1
    @Lukr if you want to run the loaddata command programmatically, you can disconnect the signals you that want to disable with Signal.disconnect() command, run the loaddata command then reconnect the signals Signal.connect(), just an alternative to using raw. This will give you a full control of the process – Majali Aug 31 '22 at 12:12
2

It looks like you've generated fixtures that include Django's default data set, i.e. the built-in entries that are inserted normally as part of the first migrate run for some of Django's plumbing data types.

You should review your fixture process, because content type entries will be created automatically when your (and Django's) apps' migrations are run, so they should not be present in fixtures. It's possible there are other tables that will have this same problem, so now would be a good time to make sure you're not including any other data that would result in this situation.

kungphu
  • 4,592
  • 3
  • 28
  • 37