Minimax
Minimax

Reputation: 131

BEFORE TRIGGER not updating value postgresql

I am trying to implement a BEFORE trigger, which updates a field to current date if another field is updated. Even though the updated value gets assigned to NEW.zuletzt, whenever I call Select * from hoert, I still see the older zuletzt value. Here is the trigger function:

CREATE OR REPLACE FUNCTION changedAnzhoert() RETURNS TRIGGER AS $$
DECLARE
    currDate DATE:=CURRENT_DATE;
BEGIN 
    IF OLD.anzahl<NEW.anzahl THEN
        NEW.zuletzt:=currDate;
        raise notice 'Neues Datum: %', NEW.zuletzt;
    END IF;

    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

And the trigger:

CREATE TRIGGER trhoertChange BEFORE UPDATE ON hoert
FOR EACH ROW EXECUTE PROCEDURE changedAnzhoert();

When I run:

UPDATE hoert SET anzahl=anzahl+1 WHERE id=1;

I see:

NOTICE: Neues Datum: 2019-01-03
UPDATE 1

Upvotes: 2

Views: 286

Answers (1)

klin
klin

Reputation: 121494

It's likely that you have another trigger. In psql execute

\d hoert 

and check whether the trigger is the only one.

Upvotes: 2

Related Questions