Search code examples
djangoheroku-postgres

Django project on Heroku initial data fixture integrityerror


I have deployed my project to Heroku and currently trying to load the data dump from local sqlite database to the Heroku database. The remote database is clean and untouched other than the initial migrate command. I have tried the following combinations of dump but all of them returned an error

python manage.py dumpdata --exclude contenttypes --> data.json
python manage.py dumpdata --exclude auth.permission --exclude contenttypes --indent 2 > data.json
python manage.py dumpdata --exclude auth.permission --exclude contenttypes --exclude auth.user  --indent 2 > data.json

and the error is:

django.db.utils.IntegrityError: Problem installing fixture '/app/data.json': Could not load wellsurfer.Profile(pk=6): duplicate key value violates unique constraint "wellsurfer_profile_user_id_key" DETAIL: Key (user_id)=(1) already exists.

i would like to post the json file here but it is about 120,000 lines. But i can provide specific portions if needed. The error clearly says the key exists but the database is clean in the beginning. Obviously, i am doing something very basic thing wrong and i hope you can point me in the right direction. I have tried recommendations that i found on Stackoverflow with no success. How to manage.py loaddata in Django


Solution

  • I had the same problem, and this is what worked for me

    source (local sqlite)

    python manage.py dumpdata --natural-foreign --indent 4 > datadump.json
    

    (this will include everything, even the auth app / users)

    destination (heroku postgres)

    python manage.py migrate
    python manage.py shell
    >>> from django.contrib.contenttypes.models import ContentType
    >>> ContentType.objects.all().delete()
    >>> quit()
    

    Finally, run following command to load the json data:

    python manage.py loaddata datadump.json