postgresql - Asking Postgres to add a non-existent sequence value -
i have existing postgres table, example:
create table profiles( profile_id serial primary key, num_feed integer, num_email integer, name text ); alter table profiles alter column profile_id set default nextval('profiles_profile_id_seq'::regclass); alter table profiles add constraint profiles_pkey primary key (profile_id); create index "pi.profile_id" on profiles using btree (profile_id);
and existing data, can't change, can add new ones.
insert profiles values (3, 2, 5, 'adam smith'), (26, 2, 1, 'fran morrow'), (30, 2, 2, 'john doe'), (32, 4, 1, 'jerry maguire'), (36, 1, 1, 'donald logue');
the problem when tried insert new data, postgres add minimum value (which good) on column "profile_id" failed/error when hits existent value, because value exists.
error: duplicate key value violates unique constraint "profile_id" detail: key (profile_id)=(3) exists.
is possible ask postgres add next non-existent value?
dont specify serial
field on insert sentence, let postgres generate sequence you.
insert profiles (num_feed, num_email, name) values (2, 5, 'adam smith'), (2, 1, 'fran morrow'), (2, 2, 'john doe'), (4, 1, 'jerry maguire'), (1, 1, 'donald logue');
note: can fail if after while reset sequence, like
alter sequence 'profiles_profile_id_seq' restart 1;
next insert try create 1
again , fail.
Comments
Post a Comment