我有一个UPSERT
触发器可能会更新,而不是在插入操作时插入。我已经在该表上插入了函数,并且returning id
但是它在更新而不是插入时不会返回一个id。我想在两种情况下都能得到这个id。PostgreSQL UPSERT触发返回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;
向我们显示您的代码 –
已更新为触发代码。函数只是使用'RETURNING id'的INSERT查询。 –
看起来不像完整定义,'DESCRIBE'部分缺失。 – vyegorov