我要在更新表a_b的历史记录上做一张表,所以我决定做一个看起来像这样的触发器:
CREATE OR REPLACE FUNCTION public.history_a_b()
RETURNS trigger AS
$BODY$
BEGIN
INSERT INTO history_a_b (
b_id,
quantity,
history_a_id)
VALUES (
OLD.a_id,
OLD.quantity,
?? [1]);
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.history_a_item()
OWNER TO postgres;
[1]:在这里,我不知道如何从另一个表获取ID,因为触发器将在更新表a_b时生成,而先前的数据将进入history_a_b
所需ID的生成来自触发器对表的更新:
CREATE OR REPLACE FUNCTION public.before_update_a()
RETURNS trigger AS
$BODY$
BEGIN
INSERT INTO history_a (
key,
action_date,
duration,
previous_a_id
)
VALUES (
OLD.key,
OLD.action_date,
OLD.duration,
OLD.id);
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.before_update_a()
OWNER TO postgres;
当我想到[1]必须是NEW.history_a.id
时,但我的更新在表a_b中...