Answer the question
In order to leave comments, you need to log in
Postgres didn't update sequence on insert from PK?
Why didn't the sequence increase by one when inserting with the specified primary key?
Answer the question
In order to leave comments, you need to log in
Unlike muscle, where auto-increment is built into the giblets at the engine level, a sequence is just a separate counter
. In order for it to work as auto-increment, you need to get the value of a specific counter each time, this is done by assigning a default value to the primary key of the table as a call to the nextval function.
id integer NOT NULL DEFAULT nextval('"some_id_seq"'::regclass)
default only evaluates if this field was not passed in the insert or was passed with the value default.
That is, if you insert into the table with an explicit indication of id - default is not executed, the sequence does not change. So in this case, you need to explicitly set the value of the counter through setval
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question