[Django]-Django 1.4 primary key conflict after a bulk_create

6๐Ÿ‘

โœ…

I solved this problem by setting the next id in the auto increment logic of postgresql, outside of Django, using the setval command, similar to questions Postgresql setting next id to write to and How to reset postgres' primary key sequence when it falls out of sync?.

In my example, i would have to execute in psql the command

select setval('keyword_id_seq', 3); 

where keyword_id_seq is the name of the auto-increment sequence (by default, <table_name>_<column_name>_seq' ), and 6965 is the maximum id present in the table. That way,
the auto-increment starts from the next id (4 in this case) and no conflict happens.

2๐Ÿ‘

You can override the default model behaviour of including an auto-incrementing primary key by adding your own id field and marking it as the primary key. E.g:

 class Keyword(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100) 

Docs: Automatic Primary Keys

Leave a comment