삽입 작업을 수행하는 동안 삽입하는 대신 업데이트 할 수있는 테이블에 UPSERT
트리거가 있습니다. 해당 테이블에 삽입 기능을했습니다 및 returning 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