PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Perché le sequenze non vengono aggiornate quando COPY viene eseguito in PostgreSQL?

Chiedi:

Sì, dovresti, come documentato qui :

Scrivi tu:

Ma non è così! :) Quando si esegue un normale INSERT, in genere non si specifica un valore esplicito per la chiave primaria supportata da SEQUENCE. Se lo facessi, ti imbatteresti negli stessi problemi che stai riscontrando ora:

postgres=> create table uh_oh (id serial not null primary key, data char(1));
NOTICE:  CREATE TABLE will create implicit sequence "uh_oh_id_seq" for serial column "uh_oh.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "uh_oh_pkey" for table "uh_oh"
CREATE TABLE
postgres=> insert into uh_oh (id, data) values (1, 'x');
INSERT 0 1
postgres=> insert into uh_oh (data) values ('a');
ERROR:  duplicate key value violates unique constraint "uh_oh_pkey"
DETAIL:  Key (id)=(1) already exists.

Il tuo comando COPY, ovviamente, fornisce un id esplicito valore, proprio come l'esempio INSERT sopra.