我UPSERT
在执行插入操作时可能会更新而不是插入的表上有一个触发器。我已经在该表上执行插入功能,returning id
但是它在更新而不是插入时不会返回 id。我想在这两种情况下都得到 id。
触发代码
perform 1 from tera_subject
where id = new.subject_id and owner = new.user_id;
if found then
return null;
else
select id into vote_id from tera_votes where
user_id = new.user_id and
subject_id = new.subject_id;
if not found then
return new;
else
-- raise notice 'vote_id: % ; vote: %',vote_id,new.vote;
if(tg_op = 'INSERT') then
begin
-- raise notice 'redirecting to update';
update tera_votes
set vote=new.vote
WHERE id = vote_id;
end;
elsif(tg_op = 'UPDATE') then
-- raise notice 'in update';
return new;
end if;
-- raise notice 'end of trigger %',tg_op;
return null;
end if;
end if;
end;