Postgres:在冲突更新时插入多行不起作用
Postgres: insert multiple rows on conflict update not working
我正在尝试将多行插入到 table 中,如果发生冲突,只需更新它们即可。我做错了什么?
insert into segments(id, departure_hour)
values
(1153, 2),
(1156, 4),
(1154, 2)
on conflict do update set
departure_hour = c.departure_hour
from (values
(1153, 2),
(1156, 4),
(1154, 2))
as c(id, departure_hour)
where c.id = segments.id
根据要求,这是我的 table 定义:
CREATE TABLE segments (
id SERIAL PRIMARY KEY,
route_id integer NOT NULL REFERENCES routes(id),
origin_id integer NOT NULL REFERENCES stops(id),
destination_id integer NOT NULL REFERENCES stops(id),
price integer DEFAULT 0,
departure_day integer NOT NULL,
departure_hour integer NOT NULL,
departure_minute integer NOT NULL,
arrival_day integer NOT NULL,
arrival_hour integer NOT NULL,
arrival_minute integer NOT NULL,
hidden boolean NOT NULL DEFAULT false,
deleted boolean NOT NULL DEFAULT false,
CONSTRAINT unique_origin_destination_per_route UNIQUE (route_id, origin_id, destination_id)
);
这是我的错误:
ERROR: syntax error at or near "from"
LINE 1: ...pdate set departure_hour = c.departure_hour from (valu...
您不需要 from
部分即可引用要更新的值。
insert into segments(id, departure_hour)
values
(1153, 2),
(1156, 4),
(1154, 2)
on conflict do update set
departure_hour = excluded.departure_hour;
我正在尝试将多行插入到 table 中,如果发生冲突,只需更新它们即可。我做错了什么?
insert into segments(id, departure_hour)
values
(1153, 2),
(1156, 4),
(1154, 2)
on conflict do update set
departure_hour = c.departure_hour
from (values
(1153, 2),
(1156, 4),
(1154, 2))
as c(id, departure_hour)
where c.id = segments.id
根据要求,这是我的 table 定义:
CREATE TABLE segments (
id SERIAL PRIMARY KEY,
route_id integer NOT NULL REFERENCES routes(id),
origin_id integer NOT NULL REFERENCES stops(id),
destination_id integer NOT NULL REFERENCES stops(id),
price integer DEFAULT 0,
departure_day integer NOT NULL,
departure_hour integer NOT NULL,
departure_minute integer NOT NULL,
arrival_day integer NOT NULL,
arrival_hour integer NOT NULL,
arrival_minute integer NOT NULL,
hidden boolean NOT NULL DEFAULT false,
deleted boolean NOT NULL DEFAULT false,
CONSTRAINT unique_origin_destination_per_route UNIQUE (route_id, origin_id, destination_id)
);
这是我的错误:
ERROR: syntax error at or near "from"
LINE 1: ...pdate set departure_hour = c.departure_hour from (valu...
您不需要 from
部分即可引用要更新的值。
insert into segments(id, departure_hour)
values
(1153, 2),
(1156, 4),
(1154, 2)
on conflict do update set
departure_hour = excluded.departure_hour;