PostgreSQL AFTER UPDATE触发器OLD没有字段

时间:2018-10-09 03:21:39

标签: postgresql postgresql-10

我有一个触发器,就像许多其他触发器一样,它们在更新后会触发并检查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

1 个答案:

答案 0 :(得分:0)

在功能中添加以下诊断行:

RAISE NOTICE 'Called % % on %',
   TG_WHEN, TG_OP, TG_TABLE_NAME;

然后运行调用触发器的SQL语句,并查看输出是否符合您的期望。