首页  >  问答  >  正文

python - 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不是自动寻找最大值,然后自增么?

PHP中文网PHP中文网2716 天前421

全部回复(1)我来回复

  • 怪我咯

    怪我咯2017-04-18 09:46:40

    这里我得到了答案当postgres不同步时如何重置主键序列?对于这个问题

    我的出了点问题id sequence.

    我通过这样做修复了它。

    雷雷

    => 11518

    雷雷

    =>第2324章 雷雷

    => 11518#修复它

    雷雷

    => 11519#完成!

    回复
    0
  • 取消回复