2017-03-08 100 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