我想知道是否有可能弄清楚postgres upsert查询是否导致插入或更新。
像这样的psuedocode:
insert into teammates (id, email, avatar, timezone)
values ($1, $2, $3, $4)
on conflict (id) do update set
avatar = $3,
timezone = $4
returning id, (updated = didUpdate);
答案 0 :(得分:0)
手动检查或执行SELECT
后跟UPSERT
以下列的方法,然后查看。
avatar = $3,
timezone = $4
您还可以拥有一个名为DATETIME
的{{1}}列,该列应在每次LastModified
操作时更新。那么你就可以找到它了。
答案 1 :(得分:0)
有必要进行手动CTE upsert:
with u as (
update teammates
set (avatar, timezone) = ($3, $4)
where id = $1
returning id, true as updated
), i as (
insert into teammates (id, email, avatar, timezone)
select $1, $2, $3, $4
where not exists (select 1 from u)
returning id, false as updated
)
select id, updated from u
union all
select id, updated from i