PostgreSQL duplicate key violates unique constraint

PostgreSQL About 390 words

错误原因

当前序列的值小于了目前表中的值。

查看序列

select * from post_id_seq;

输出

 last_value | log_cnt | is_called 
------------+---------+-----------
      11563 |      30 | t
(1 row)

查看 id 最大值

select max(id) from post;

解决方法

序列post_id_seq为表post的字段id的默认nextval(),设置当前的序列post_id_seq为目前表post中最大的id值。如:post最大值为10,则下次nextval返回11

注意:select max(id) ...必须用括号括起来

select setval('post_id_seq', (select max(id) from post));
Views: 2,176 · Posted: 2020-12-15

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh