P
P
P7472020-10-27 11:33:57
PostgreSQL
P747, 2020-10-27 11:33:57

PostgreSQL, why is the "TG_TABLE_NAME" variable not being substituted?

Good afternoon!

Why does an error occur and the data from the variable is not substituted?

5f97db56c073e358125368.png

Trigger and function code:

CREATE OR REPLACE FUNCTION update_dt_column() RETURNS TRIGGER AS $$
BEGIN
    UPDATE TG_TABLE_NAME SET dt_update = NOW() WHERE id = NEW.id;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER t_phones_dt_update
AFTER UPDATE ON phones FOR EACH ROW EXECUTE PROCEDURE update_dt_column();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2020-10-27
@P747

Because why did you decide that pl/pgsql has an update variable set syntax? He is not here. This place is always the name of the table.
However, the trigger is completely absurd and will not generate anything other than an infinite recursion.
You need a before insert trigger with

NEW.dt_update = NOW();
return NEW;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question