14

Took over a database project and I am struggling to load the remote database into the local database.

The app was built with django and the local database still relies on sqlite that comes with out of the box.

The remote database is of postgresql type.

The code I am trying to run in the terminal:

python manage.py loaddata *[path to backup.json file]*

I get some integrity error so like any reasonable man I flushed the local db because since I want to anyhows load the remote data.

python manage.py flush python manage.py syncdata

Now when I try to load the data from the json file I get the following error:

django.db.utils.IntegrityError: Problem installing fixture 'C:...\lit\backups\dbbackup_20190915_145546.json': Could not load contenttypes.ContentType(pk=1): UNIQUE constraint failed: django_content_type.app_label, django_conten t_type.model

Changing the settings.py file from:

`DATABASES = {
    'default':  {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}`

to

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'lit',
        'USER': 'admin',
        'PASSWORD': 'admin',
        'HOST': 'localhost',
        'PORT': '5432'
    }

just gives me a new error.

django.db.utils.IntegrityError: Problem installing fixture 'C:..\lit\backups\dbbackup_20190915_145546.json': Could not load contenttypes.ContentType(pk=17): duplicate key value violates unique constraint "django_content_type_a pp_label_model_76bd3d3b_uniq" DETAIL: Key (app_label, model)=(admin, logentry) already exists.

I already ran

python manage.py makemigrations
python manage.py migrate
1
  • For me, signal was creating object before it actually created from dumped data. For ex, there is table A and table B. here, signal is to create object of table B on pre_save of table A, but in dumped data table A was first delared so it automatically creates object of table B, then from dumped data for second object it'll try to create object B but its already created so I fixed signal accordingly. Commented Dec 22, 2022 at 6:22

1 Answer 1

29

in your local database you create some ContentType instances.

when you migrate your remote database all ContentType for your models created again.

but when you want to load data you try to load this instances again.

you have 2 solutions

1- remove all content types instances from remote host using django shell

python manage.py shell

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

2- remove content type instances from dumped data

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

3 Comments

thank you. How to avoid this happening again? Where could I look for the error and correct it?
you must avoid exporting content_type table content to fixture files
@vorujack Excluding content_type can lead to a mismatch for any polymorphic tables. I am running into this now. As my production application appends new tables to content_type vs loading a new instance of production locally will group all content_types by model. (I am still looking for a nice solution for this)

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.