Postgresql不会通过触发器更新上次更新的行

时间:2017-12-05 10:04:48

标签: postgresql

我创建了一个触发器。当我更新一行之前,触发器不起作用。稍后,当我第二次更新行时触发器正在工作。为什么以前不工作?

CREATE TRIGGER trg_update_em_z_module_mod_codes
  before UPDATE
ON emlakmuayene.em_z_module
FOR EACH ROW
  EXECUTE PROCEDURE emlakmuayene.update_em_z_module_mod_codes();

CREATE OR REPLACE FUNCTION emlakmuayene.update_em_z_module_mod_codes()
  RETURNS trigger AS
$BODY$
    BEGIN   
        if (TG_OP = 'UPDATE') then 
        if new.mod_codes_ids is not null  then      
        new.mod_codes=
        (
        SELECT translate(array_Agg(pages)::Text,'{}','') as mod_codes 
        FROM emlakmuayene.em_z_module  t1 
        join emlakmuayene.em_z_order_pages_view  t2 
        on t2.id_seq_x = any ( ('{'||t1.mod_codes_ids||'}')::bigint[]) where t1.id_Seq_X=new.id_seq_x  group by t1.id_Seq_X );          
        end if ;
        end if;
    RETURN new;
    END;    
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

1 个答案:

答案 0 :(得分:0)

添加我的触发器IF pg_trigger_depth() <> 1 THEN RETURN NEW;后 并改变我的             update sql update emlakmuayene.em_z_module t1 set mod_codes = t2.mod_codes ,reg=1 from ( SELECT t1.id_Seq_x ,translate(array_Agg(pages)::Text,'{}','') as mod_codes FROM emlakmuayene.em_z_module t1 join emlakmuayene.em_z_order_pages_view t2 on t2.id_seq_x = any ( ('{'||t1.mod_codes_ids||'}')::bigint[]) where t1.id_seq_x=new.id_seq_x group by t1.id_Seq_X ) t2 where t1.id_seq_x=T2.id_seq_x;正在运行

然后我的触发器 CREATE OR REPLACE FUNCTION emlakmuayene.update_em_z_module_mod_codes() RETURNS trigger AS $BODY$ BEGIN
IF pg_trigger_depth() <> 1 THEN RETURN NEW; END IF; update emlakmuayene.em_z_module t1 set mod_codes = t2.mod_codes ,reg=1 from ( SELECT t1.id_Seq_x ,translate(array_Agg(pages)::Text,'{}','') as mod_codes FROM emlakmuayene.em_z_module t1 join emlakmuayene.em_z_order_pages_view t2 on t2.id_seq_x = any ( ('{'||t1.mod_codes_ids||'}')::bigint[]) where t1.id_seq_x=new.id_seq_x group by t1.id_Seq_X ) t2 where t1.id_seq_x=T2.id_seq_x; return new; END;
$BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION emlakmuayene.update_em_z_module_mod_codes() OWNER TO tuvimer_ortak