I’m trying to "copy/paste" rows of a table with bigserial
id
column, without listing all columns names in the query. There is a related post https://stackoverflow.com/a/15344327, which does not work with psql.
The isses is that psql does not trigger auto increment, when inserting NULL values.
The minimal case is
CREATE TABLE src (
id bigserial,
txt text NOT NULL);
INSERT INTO src (
txt)
VALUES (
'a'),
(
'b'),
(
'c'),
(
'b'
);
CREATE temp TABLE src_temp AS
SELECT
*
FROM
src
WHERE
txt = 'b';
UPDATE
src_temp
SET
id = NULL;
INSERT INTO src
SELECT
*
FROM
src_temp;
resulting in: ERROR: null value in column "id" of relation "src" violates not-null constraint Detail: Failing row contains (null, b).
The practical case is that there are way more columns in the src
table.
Any way to make it?
Cheers,
AAWNSD
2
Answers
You need to use correct values in your
UPDATE
query. To do this you first need to find out the identifier of the sequence that works the magic of yourbigserial
column. Usually it is tablename underscore columnname underscore ‘seq’, so for your example it would most likely besrc_id_seq
.Then you need to modify the
UPDATE
statement so that it fills in values from this sequence instead ofNULL
usingnextval()
:Now you have valid datasets in
src_temp
that you can insert intosrc
.Note that if your tablename or columnname needs quoting, i.e. because they contain uppercase letters, you end up with an identifier for the sequence that also needs quoting like this
Constraints are checked before the update and the autoincrement is triggered.
You are explicitly adding
NULL
toid
. The issue is that serial types addNOT NULL
to the column definition so that constraint kicks in when the INSERT is attempted. If you want this to work doINSERT INTO src(txt) SELECT txt FROM src_temp;
. Then theid
column will be populated by theDEFAULT
value which is thenextval()
of the sequence backing thebigserial
type.