postgresql 插入时报错ID已存在
作插入操作:
name = 'test'
cur.execute("INSERT INTO scholars(name) VALUES('{}') returning id".format(name))
id = cur.fetchone()
print(id)
报错如下:
psycopg2.IntegrityError: duplicate key value violates unique constraint "idx_16514_primary"
DETAIL: Key (id)=(2321) already exists.
id=2301
时插入成功,并且成功返回ID。
之后插入一次,id+1,报错ID已存在。
数据库插入操作,ID不是自动寻找最大值,然后自增么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Here i got the answer How to reset postgres' primary key sequence when it falls out of sync? to this question.
Something was wrong with my
id sequence
.I fixed it by doing this.
=> 11518
=> 2324 # here it is lower than max(id)
=> 11518 # fix it
=> 11519 # done!