0
我有一个表像发送此更新计数器触发的PostgreSQL
-----------------------------------------------------------------------------------
id | notification_sent | times_notification_sent
-----------------------------------------------------------------------------------
4a1717a2-6e1e-4af6-aa34-8262899aa060 | t | 0
这里通知是布尔字段和times_notification_sent为int。我正在尝试创建触发器,当notification_sent从true更改为false时,会将times_notification_sent递增1。
我正在使用下面的函数和触发器,但它不工作。
CREATE OR REPLACE FUNCTION update_sent_counter_for_watch() RETURNS TRIGGER
LANGUAGE plpgsql
AS $$ BEGIN
IF OLD.notification_sent IS TRUE AND NEW.notification_sent IS FALSE THEN
UPDATE "watch" SET "times_notification_sent" = "times_notification_sent" + 1 WHERE "id" = OLD."id";
END IF;
END;
$$;
CREATE TRIGGER "update_times_sent_counter" AFTER UPDATE OF "times_notification_sent" ON "public"."watch"
FOR EACH ROW
WHEN (OLD.notification_sent IS DISTINCT FROM NEW.notification_sent)
EXECUTE PROCEDURE "public"."update_sent_counter_for_watch"();
你们俩都是corr等。非常感谢您的帮助。 – nicholasnet
@帕特里克:谢谢,纠正 –