22 Kasım 2021

PostgreSQL Sequence Set


ERROR: duplicate key value violates unique constraint "pkey"

DETAIL: Key (id)=(1503) already exists. SQL state: 23505



SELECT MAX(the_primary_key) FROM the_table;   
SELECT nextval('the_primary_key_sequence');


Get Sequence Name

 select pg_get_serial_sequence('tableName','columnName')


Set Sequence Name
SELECT setval('the_primary_key_sequence', (SELECT MAX(the_primary_key) FROM the_table)+1);

VEYA
SELECT setval(pg_get_serial_sequence('tableName','columnName'), (SELECT MAX(columnName) FROM tableName)+1);