Skip to content Skip to sidebar Skip to footer

Django Import-export Import Duplicate Key Value Violates Error

I'm working on a project with Django 1.10, Python 3.6 and PostgreSQL as the database backend, in which I have a model called 'Article' and I need to import data from CSV files. I h

Solution 1:

I had this issue, because I'd specified a custom import_id_fields field but hadn't excluded the id field.

I had specified:

import_id_fields = ('code',)
    skip_unchanged = Truereport_skipped = True

But I needed:

import_id_fields = ('code',)
    skip_unchanged = Truereport_skipped = Trueexclude = ('id',)

Bit of a noob error but this might save someone a google.

Ref: https://github.com/django-import-export/django-import-export/issues/273

Solution 2:

I have triggered that what's the issue : Actually, the problem is PostgreSQL primary key sequential which is not synced with table rows. That's why, when I insert a new row I get a duplicate key error because the sequence implied in the serial datatype returns a number that already exists.

To solve this issue we have to reset the ID sequential for PostgreSQL, Here's the step by step guide:

  1. Log onto the DB shell and connect to your database
  2. First, check maximum value for id column of your table as SELECT MAX(id) FROM your_table;
  3. Then, check what's going to be the next value for ID as : SELECT nextval('your_table_id_seq');
  4. If nextval is the next number of Max value then it's right. e.g MAX=10 & nextval=11
  5. Otherwise reset the id_seq for your table as:

    BEGIN;

    -- protect against concurrent inserts while you update the counter

    LOCK TABLE your_table IN EXCLUSIVE MODE;

    -- Update the sequence

    SELECT setval('your_table_id_seq', COALESCE((SELECT MAX(id)+1 FROM your_table), 1), false);

    COMMIT;

Solution 3:

i often run a for loop. This also updates my auto datetime fields:

import Article
for i in range(1, your_last_id + 1):
    item = Article.objects.get(id=i)
    item.save()

your_last_id may be SELECT MAX(id)

Post a Comment for "Django Import-export Import Duplicate Key Value Violates Error"