문제

I'm new to postgres. I defined a function like this:

CREATE OR REPLACE FUNCTION increment_count() 
  RETURNS TRIGGER LANGUAGE plpgsql 
  AS $$
   BEGIN 
     UPDATE posts SET count=count+1 WHERE pid=NEW.pid;
   COMMIT;
   END;
  $$;

This gives me error invalid transaction termination

도움이 되었습니까?

해결책

You can not commit in a trigger, and you forgot the return statement.

CREATE OR REPLACE FUNCTION increment_count() 
  RETURNS TRIGGER LANGUAGE plpgsql 
  AS $$
    BEGIN 
      UPDATE posts SET count=count+1 WHERE pid=NEW.pid;
      return null;
    END;
  $$;

In case of a BEFORE trigger, "return NEW" should be used!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top