在Update事件期间触发失火 [英] Trigger misfiring during Update event

查看:90
本文介绍了在Update事件期间触发失火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如果我从Backend执行,但是当我从前端执行相同操作时触发器工作正常,它适用于插入和删除但是失败更新..



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



这是我的触发功能



创建或替换函数audit_track()返回触发器

as $ body $



begin

如果tg_op ='UPDATE'那么



插入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。*)));

插入query_relation值(txid_current(), current_query(),now());

返回new;

elsif tg_op ='DELETE'然后

插入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。*)));

插入query_relation值(txid_current(),current_query(),now());

返回旧;

elsif tg_op ='INSERT'然后



插入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。*)));

插入query_relation值(txid_current(),current_query(),now());

返回new ;

结束if;

EXCEPTION

当data_exception那么



插入exception_details值(current_query(),sqlerrm,now());

返回新的;

WHEN unique_violation那么



插入exception_details值(current_query(),sqlerrm,now());

返回新功能;

其他等等



插入exception_details值(current_query(),sqlerrm,now());

返回新的;

结束;

$ body $

language plpgsql;



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

I have a Audit Trigger created which is as below. I'm using PostgreSQL 9.3.

The Trigger works perfectly if i execute from Backend but when I do the same from Frontend,It works fine for Insert & Delete but fails for Update..

Also in my code,I hav given inserts to two tables mapped by Foreign key relation.For Update event,I get wrongly updated columns and sometimes entry is there in 2nd table(query_relation) but for that there is no entry in 1st table(audit_track).

Here is mine trigger function

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;

Am i doing something wrong for update event to fail from frontend but to work properly if done manually from backend???

推荐答案

body






begin

if tg_op ='UPDATE'然后



插入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。*)));

插入query_relation值(txid_current (),current_query(),now());

返回new;

elsif tg_op ='DELETE'然后

插入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。*)));

插入query_relation值(txid_current(),current_query(),now());

返回旧;

elsif tg_op ='INSERT'然后



插入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() ,'插入',svals(hstore(new。*)),skeys(hstore(new。*)));

插入query_relation值(txid_current(),current_query(),now() );

返回新的;

结束if;

EXCEPTION

当data_exception那么



插入exception_details值(current_query(),sqlerrm,now());

返回新的;

WHEN unique_violation那么



插入exception_details值(current_query(),sqlerrm,now());

返回新功能;

其他等等



插入exception_details值(current_query(),sqlerrm,now());

返回新的;

结束;


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


这篇关于在Update事件期间触发失火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆