私は、挿入操作中に挿入するのではなく更新する可能性があるテーブルに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