I have such table:
class Tag(Base): __tablename__ = 'tag' id = Column(Integer, primary_key=True) name = Column(Unicode(50), nullable=False) def __init__(self, name): self.name = name Supose table is empty. Than I insert some data in such way:
t = Tag('first tag') t.id = 2 dbsession.add(t) dbsession.commit() And it's ok: I have one row in my postgresql table with id = 2. Next:
t = Tag('second tag') dbsession.add(t) dbsession.commit() Than I have 2 rows: {id: 1, name: 'second tag'} and {id: 2, name: 'first_tag'}
Next (final step):
t = Tag('third tag') dbsession.add(t) dbsession.commit() IntegrityError: (IntegrityError) duplicate key value violates unique constraint "tag_pkey"
(it wants to create row with id = 2, but it is engaged already with first tag)
Is it possible somehow to say to postgres: if such pkey is exist, try next until it will be available?
It is very usefull when using dumps.
Thanks in advance!
serialcolumn or you provide a value in every single insert. The other way is to reset the correspondig sequence so that the nextINSERTwill get the correct value.