postgres autoincrement not updated on explicit id inserts

前端 未结 3 1713
清酒与你
清酒与你 2020-12-04 21:26

I have the following table in postgres:

CREATE TABLE \"test\" (
    \"id\" serial NOT NULL PRIMARY KEY,
    \"value\" text
)

I am doing fol

3条回答
  •  暖寄归人
    2020-12-04 21:36

    That's how it's supposed to work - next_val('test_id_seq') is only called when the system needs a value for this column and you have not provided one. If you provide value no such call is performed and consequently the sequence is not "updated".

    You could work around this by manually setting the value of the sequence after your last insert with explicitly provided values:

    SELECT setval('test_id_seq', (SELECT MAX(id) from "test"));
    

提交回复
热议问题