为什么 postgresql 触发器不启动?

Why postgresql trigger doesn't launch?

我使用 postgresql 11。 我认为我的问题来自不执行更新的触发器,因此下一个触发器不会启动。

我有一个 table projet 列:projet_temps_doeprojet_temps_etudesprojet_temps_globale.

目标是根据其他列的值更新每一列。 这个想法是:projet_temps_globale = projet_temps_doe + projet_temps_etudes.

我在 projet_temps_doe 上有第一个触发器,效果很好:

create function temps_globale_doe() returns trigger
    language plpgsql
as
$$
begin
        new.projet_temps_globale_doe :=  new.projet_temps_doe_gc_bts + new.projet_temps_doe_gc_nra;
        return new;
    end;
$$;

CREATE TRIGGER temps_globale_doe
    BEFORE UPDATE OF projet_temps_doe_gc_bts, projet_temps_doe_gc_nra
    ON public.projet
    FOR EACH ROW
    EXECUTE PROCEDURE public.temps_globale_doe();

我在 projet_temps_etudes 上有一个类似的触发器,它也非常有效。

然后我在 projet_temps_globale 上遇到的触发器:

create trigger maj_temps_globale_projet
    before update of projet_temps_doe, projet_temps_etudes on projet
    for each row
    execute procedure maj_temps_globale_projet();

create or replace function maj_temps_globale_projet()returns trigger
language plpgsql
as
    $$
    begin
        new.projet_temps_globale := new.projet_temps_doe + new.projet_temps_etudes;
        raise info 'TEST!!';
        return new;
    end;
    $$;

projet_temps_doe and/or projet_temps_etudes 通过触发器更新时,我的最后一个触发器不会启动。但是,当我手动更改 projet_temps_doe and/or projet_temps_etudes 值时,触发器 maj_temps_globale_projet 被触发。

我想从中吸取教训,所以,如果可能的话,请向我解释我在这里做错了什么,或者我的方法是否缺乏洞察力。

doc

The trigger will only fire if at least one of the listed columns is mentioned as a target of the UPDATE command.

projet_temps_globale_doe 不是更新命令的一部分,而是通过 new.projet_temps_globale_doe = ... 在另一个触发器中设置的,因此不会调用此特定列上的触发器。

整个 table 上只有一个触发器会更容易设置 3 个派生值。