更新触发器不会从前端失火

时间:2015-05-25 04:41:35

标签: postgresql-9.3

我创建了一个审核触发器,如下所示。我正在使用PostgreSQL 9.3。

如果我从Backend执行,但是当我从前端执行相同操作时触发器工作正常,它适用于Insert&删除但无法更新..

同样在我的代码中,我给了两个由外键关系映射的表的插入。对于Update事件,我得到错误更新的列,有时候在第二个表(query_relation)中有条目但是为了那个,第一个没有条目表(audit_track)。

这是我的触发功能

create or replace function audit_track() returns trigger 
as $body$

begin
if tg_op = 'UPDATE' then

insert into audit_track (audit_id,table_name, audit_by,audit_date, action, old_values, new_values, updated_columns)
values (txid_current(),tg_table_name::text,new.audit_by,now(), 'Update',   svals (hstore(old.*) - hstore(new.*)) ,  svals (hstore(new.*) - hstore(old.*)),

         skeys  (hstore(new.*) - hstore(old.*)));
insert into query_relation values(txid_current(),current_query(),now());
return new;
elsif tg_op = 'DELETE' then
insert into audit_track (audit_id,table_name, audit_by,audit_date, action, old_values,updated_columns)

values (txid_current(),tg_table_name::text, old.audit_by,now(), 'Delete',  svals (hstore(old.*)),skeys (hstore(old.*)));
insert into query_relation values(txid_current(),current_query(),now());
return old;
elsif tg_op = 'INSERT' then

insert into audit_track(audit_id,table_name, audit_by,audit_date, action, new_values,updated_columns)
values (txid_current(),tg_table_name::text, new.audit_by,now(), 'Insert',  svals (hstore(new.*)),skeys (hstore(new.*)));
insert into query_relation values(txid_current(),current_query(),now());
return new; 
end if;
EXCEPTION
WHEN data_exception THEN

insert into exception_details values(current_query(),sqlerrm,now());
RETURN New;
WHEN unique_violation THEN

insert into exception_details values(current_query(),sqlerrm,now());
RETURN New;
WHEN OTHERS THEN 

insert into exception_details values(current_query(),sqlerrm,now());
RETURN New; 
end;
$body$
language plpgsql; 

我做错了更新事件从前端失败但是如果从后端手动完成工作???

0 个答案:

没有答案