Postgresql AFTER UPDATE 触发器 OLD 没有字段
Postgresql AFTER UPDATE trigger OLD has no field
我有一个触发器,就像许多其他在更新后触发并检查 OLD
集的字段的触发器一样。
由于某种原因,此触发器抛出此错误:
ERROR: record "old" has no field "status"
CONTEXT: SQL statement "SELECT NOT (OLD.status = NEW.status) AND NEW.status = 'Success'"
这是触发器的主体:
CREATE OR REPLACE FUNCTION tr_online_payment_after_update()
RETURNS trigger AS
$BODY$
DECLARE
v_rec RECORD;
BEGIN
-- If state changed to Success
IF NOT (OLD.status = NEW.status) AND NEW.status = 'Success' THEN
-- Find any invoices attached and set them to paid
FOR v_rec IN
SELECT invoice_id_fk
FROM online_payment_invoice
WHERE online_payment_id_fk = NEW.online_payment_id
LOOP
UPDATE invoice
SET paid_date = CURRENT_DATE,
updator_id_fk = -2,
updated = LOCALTIMESTAMP
WHERE invoice_id = v_rec.invoice_id_fk;
END LOOP;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE TRIGGER tr_online_payment_after_update
AFTER UPDATE
ON online_payment
FOR EACH ROW
EXECUTE PROCEDURE tr_online_payment_after_update();
奇怪的是,它实际上似乎 运行 触发器的发票更新部分。
我看不到我在这里遗漏了什么。没有意义。
这是完整的输出:
=>UPDATE online_payment SET status = 'Success' WHERE online_payment_id = 18;
ERROR: record "old" has no field "status"
CONTEXT: SQL statement "SELECT NOT (OLD.status = NEW.status) AND NEW.status = 'Success'"
PL/pgSQL function tr_online_payment_after_update() line 7 at IF
SQL statement "UPDATE load_unit
SET load_hinderance = load_hinderance(load_unit_id)
WHERE load_consign_match_id_fk = v_lcm_id"
PL/pgSQL function tr_invoice_after_update() line 50 at SQL statement
SQL statement "UPDATE invoice
SET paid_date = CURRENT_DATE,
updator_id_fk = -2,
updated = LOCALTIMESTAMP
WHERE invoice_id = v_rec.invoice_id_fk"
PL/pgSQL function tr_online_payment_after_update() line 14 at SQL statement
将以下诊断行添加到函数中:
RAISE NOTICE 'Called % % on %',
TG_WHEN, TG_OP, TG_TABLE_NAME;
然后运行调用触发器的SQL语句并查看输出是否符合您的预期。
我有一个触发器,就像许多其他在更新后触发并检查 OLD
集的字段的触发器一样。
由于某种原因,此触发器抛出此错误:
ERROR: record "old" has no field "status"
CONTEXT: SQL statement "SELECT NOT (OLD.status = NEW.status) AND NEW.status = 'Success'"
这是触发器的主体:
CREATE OR REPLACE FUNCTION tr_online_payment_after_update()
RETURNS trigger AS
$BODY$
DECLARE
v_rec RECORD;
BEGIN
-- If state changed to Success
IF NOT (OLD.status = NEW.status) AND NEW.status = 'Success' THEN
-- Find any invoices attached and set them to paid
FOR v_rec IN
SELECT invoice_id_fk
FROM online_payment_invoice
WHERE online_payment_id_fk = NEW.online_payment_id
LOOP
UPDATE invoice
SET paid_date = CURRENT_DATE,
updator_id_fk = -2,
updated = LOCALTIMESTAMP
WHERE invoice_id = v_rec.invoice_id_fk;
END LOOP;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE TRIGGER tr_online_payment_after_update
AFTER UPDATE
ON online_payment
FOR EACH ROW
EXECUTE PROCEDURE tr_online_payment_after_update();
奇怪的是,它实际上似乎 运行 触发器的发票更新部分。
我看不到我在这里遗漏了什么。没有意义。
这是完整的输出:
=>UPDATE online_payment SET status = 'Success' WHERE online_payment_id = 18;
ERROR: record "old" has no field "status"
CONTEXT: SQL statement "SELECT NOT (OLD.status = NEW.status) AND NEW.status = 'Success'"
PL/pgSQL function tr_online_payment_after_update() line 7 at IF
SQL statement "UPDATE load_unit
SET load_hinderance = load_hinderance(load_unit_id)
WHERE load_consign_match_id_fk = v_lcm_id"
PL/pgSQL function tr_invoice_after_update() line 50 at SQL statement
SQL statement "UPDATE invoice
SET paid_date = CURRENT_DATE,
updator_id_fk = -2,
updated = LOCALTIMESTAMP
WHERE invoice_id = v_rec.invoice_id_fk"
PL/pgSQL function tr_online_payment_after_update() line 14 at SQL statement
将以下诊断行添加到函数中:
RAISE NOTICE 'Called % % on %',
TG_WHEN, TG_OP, TG_TABLE_NAME;
然后运行调用触发器的SQL语句并查看输出是否符合您的预期。