2

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

6
  • Please show us the UPDATE statement you use. Commented Jan 3, 2019 at 12:20
  • UPDATE hoert SET anzahl=anzahl+1 WHERE id=1 NOTICE: Neues Datum: 2019-01-03 UPDATE 1 Commented Jan 3, 2019 at 12:25
  • Maybe you are not committing your UPDATE? Commented Jan 3, 2019 at 12:27
  • Hmm weird, I did try it as a Transaction with BEGIN; UPDATE; COMMIT; , still no change in the table (except anzahl=anzahl+1) Commented Jan 3, 2019 at 12:30
  • 1
    @klin that was it, the trigger was initially saved as AFTER trigger for some reason. NEW cannot be updated on AFTER triggers. Thank You! Write this as answer so I can chose this as Answered! Commented Jan 3, 2019 at 12:37

1 Answer 1

2

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

\d hoert 

and check whether the trigger is the only one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.