Domanda

Consider the following schema

CREATE TYPE episode_id AS
(
    season_nr int,
    episode_nr int,
    show_id bigint
);

CREATE TABLE episodes
(
    id episode_id primary key,
    title varchar(255) not null,
    release_date timestamptz null
);

I want to insert to multiple rows in one query;

insert into episodes (id.season_nr, id.episode_nr, id.show_id, title, release_date)
values 
(1, 1, 58811, 'Pilot', null),
(1, 2, 58811, 'Vector', null),
(1, 3, 58811, '274', null),
(1, 4, 58811, 'Single Strand', null),
(1, 5, 58811, 'The White Room', null);

It throws:

> ERROR: multiple assignments to same column "id" SQL state: 42601

My client library does not support ROW syntax. How could I make this work?

È stato utile?

Soluzione

Proper syntax:

INSERT INTO episodes (id, title, release_date)
VALUES 
((1, 1, 58811), 'Pilot', null),
((1, 2, 58811), 'Vector', null);

You cannot reference individual attributes of id in the column list of an INSERT statement, just the whole column.

The added parentheses make a row type / composite type out of the three values.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top