2017-03-08 118 views
0

我想知道是否有可能找出postgres upsert查询是否导致插入或更新。检查是否插入或更新Postgres查询(通过upsert)

像这样伪码:

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

单程t1至手动检查或者执行SELECT随后UPSERT下面列和看到。

avatar = $3, 
    timezone = $4 

您可以也有DATETIME列名为LastModified应在每个UPDATE操作进行更新。那么你很容易找到答案。

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