I have a table like this
-----------------------------------------------------------------------------------
id | notification_sent | times_notification_sent
-----------------------------------------------------------------------------------
4a1717a2-6e1e-4af6-aa34-8262899aa060 | t | 0
Here notification sent is boolean field and times_notification_sent is int. I am trying to create trigger which will increment the times_notification_sent by 1 when notification_sent is changed from true to false.
I am using following function and triggers but it not working.
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"();