N
N
nurzhannogerbek2019-03-26 15:30:56
PostgreSQL
nurzhannogerbek, 2019-03-26 15:30:56

How to track a column change by a certain value in a trigger?

The PostgreSQL database has a table called SURVEYS . It looks like this:

| ID (uuid)                            | name (varchar) | status (boolean) | update_at (timestamp)    |  
|--------------------------------------|----------------|------------------|--------------------------|
| 9bef1274-f1ee-4879-a60e-16e94e88df38 | Doom           | 1                | 2019-03-26 00:00:00      |

In this table, as you can see, there are columns status (status) and update_at (last update date).
If any entry changes the value in the status column to 2 and update_at to any new value, the function must be run. In the function itself, I'm going to use the ID of the entry that was changed. Do I understand correctly that the check for the value in the status column needs to be done at the trigger level, and not at the function level? I created such a trigger, but I'm not sure how correct it is in my case.
CREATE TRIGGER СHECK_FOR_UPDATES_IN_SURVEYS
BEFORE UPDATE ON SURVEYS
FOR EACH ROW
WHEN 
  (OLD.update_at IS DISTINCT FROM NEW.update_at)
AND 
  (OLD.condition IS DISTINCT FROM NEW.condition AND NEW.condition = 2)
EXECUTE PROCEDURE CREATE_SURVEYS_QUESTIONS_RELATIONSHIP(NEW.id);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question