I would like to create my first trigger on my PostgreSQL
database but I got a syntax error
.
The trigger should be able to delete each row from token_jwt_usertoken
table where date_information
is inferior than now() - interval '2 month'
. In other words, if the row is expired from at least 2 months, the row should be deleted.
This is my command :
CREATE FUNCTION after_update_token() RETURNS TRIGGER AS $after_update_token$
BEGIN
IF old.date_information < now() - INTERVAL '2 month'
THEN
DELETE FROM token_jwt_usertoken WHERE old.date_information < now() - INTERVAL '2 month';
END IF;
END;
$after_update_token$ LANGUAGE plpgsql;
CREATE TRIGGER after_update_token AFTER UPDATE ON token_jwt_usertoken FOR EACH ROW EXECUTE FUNCTION after_update_token();
I get :
[2019-02-07 17:14:01] [42601] ERROR: syntax error at or near "FUNCTION" [2019-02-07 17:14:01] Position : 92
Then, I don't know if my request is well-written in my trigger.
Thank you !
TRIGGER
itself are separately-created objects. See the docs for examples. – Discwhen
clause – WeasnerBEGIN
allowed in the command. – CarlistFOR EACH ROW EXECUTE FUNCTION
but I think the correct syntax isFOR EACH ROW EXECUTE PROCEDURE
. – RanieEXECUTE PROCEDURE
. – DiscFUNCTION
matchingPosition : 92
is the latter one, not the one inCREATE FUNCTION
– Ranie