Django Import-export Import Duplicate Key Value Violates Error
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:
- Log onto the DB shell and connect to your database
- First, check maximum value for id column of your table as
SELECT MAX(id) FROM your_table;
- Then, check what's going to be the next value for ID as :
SELECT nextval('your_table_id_seq');
- If nextval is the next number of Max value then it's right. e.g MAX=10 & nextval=11
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"