考虑以下架构
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
);
我想在一个查询中插入多行;
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);
它抛出:
> ERROR: multiple assignments to same column "id" SQL state: 42601
我的客户端库不支持ROW
语法。我怎么能做这个工作?
答案 0 :(得分:2)
正确的语法:
INSERT INTO episodes (id, title, release_date)
VALUES
((1, 1, 58811), 'Pilot', null),
((1, 2, 58811), 'Vector', null);
您无法在id
语句的列列表中引用INSERT
的单个属性,只能引用整个列。
添加的括号从三个值中生成行类型/复合类型。