Home > Mobile >  Psycopg2 duplicate key value violates unique constraint (when migrating from SQLITE to POSTGRESQL)
Psycopg2 duplicate key value violates unique constraint (when migrating from SQLITE to POSTGRESQL)

Time:01-19

I have been trying to migrate a database from SQLite to POSTGRESQL.

I am using json fixtures to dump and load the data, I tested multiple ways but I end up in a similar situation, and I say similar because I can reach 2 slightly different errors.

So the 2 errors I can reach are the following:

django.db.utils.IntegrityError: Problem installing fixture '/PATH/wcontenttypes.json': Could not load MyApp.DIN_STATUS(pk=1): duplicate key value violates unique constraint "MyApp_din_status_DSP_id_EA_id_1c1c3e96_uniq"
DETAIL:  Key ("DSP_id", "EA_id")=(542, 20324) already exists.

The other one is the same but instead of pk=1, its pk=5

What did I check?

  1. If there's a duplicate -> but there is not
  2. If the row referenced by the id exist -> it does exist
  3. Removing the row that gives the error -> The next one gives the error (in case its pk=5, then pk=6, if pk=1 then pk=2)

What did I test?

I did multiple test looking around the internet, and testing almost anything I could find, the research ended up with 3 main ideas on how to do this

Test 1

python manage.py dumpdata > wcontenttypes.json
#-Swap to postgre
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete 
python manage.py makemigrations
python manage.py migrate
python manage.py shell
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
python manage.py loaddata wcontenttypes.json -v3

Test 2

python manage.py dumpdata > wcontenttypes.json
#-Swap to postgre
python manage.py migrate --run-syncdb
python manage.py shell
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
python manage.py loaddata wcontenttypes.json -v3

Test 3 (I played a lot more than what is shown in this example, like only excluding content types or only having natural-foreign and removing contenttypes from the shell)

python manage.py dumpdata --natural-foreign --exclude contenttypes --exclude auth.permission --exclude admin.logentry --exclude sessions.session --indent 4 > fixture.json
#-Swap to postgre
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete 
python manage.py makemigrations
python manage.py migrate
python manage.py loaddata fixture.json -v3

Right now I am pretty much lost as everything I see in the internet is something I already tested.

If you miss any info let me know and I will make an EDIT.

CodePudding user response:

So reading more posts I got to this one:

https://gist.github.com/sirodoht/f598d14e9644e2d3909629a41e3522ad

After reading some of the comments I remembered that the dev team got signals creating this din_status post_save, so they are the reason of duplicate entry.

Commenting the signals involved fixed the issue.

  • Related