D
D
des1roer2017-06-21 11:48:44
PostgreSQL
des1roer, 2017-06-21 11:48:44

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

2 answer(s)
D
Dmitry MiksIr, 2017-06-21
@des1roer

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

M
Melkij, 2017-06-21
@melkij

Because nextval was not called. For uselessness.
Since it was not necessary to generate a new value, then why pull it in vain?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question