Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raising exception in postgresql

Tags:

postgresql

Having strange error, rising from function:

CREATE OR REPLACE FUNCTION addAnswer(bodyR VARCHAR, userIdR BIGINT, qId BIGINT)
RETURNS BIGINT AS $$
DECLARE
  ret BIGINT;
  author_idR BIGINT;
    BEGIN 
    author_idR := (SELECT "a"."author_id" FROM "question" "a" WHERE "a"."id"=qId);

    IF (author_idR = userIdR ) THEN
      RAISE EXCEPTION "Question author and answer author shouldn't match!";
    ENDIF;

    INSERT INTO "answer"("body", "author_id", "created_at", "question_id") VALUES (bodyR, userIdR, CURRENT_TIMESTAMP, qId);
    ret := LASTVAL();
    RETURN ret;
  END
  $$ LANGUAGE plpgsql;

The error statement I receive:

ERROR:  unrecognized exception condition "Question author and answer author shouldn't match!"
CONTEXT:  compilation of PL/pgSQL function "addanswer" near line 9

********** Error **********

ERROR: unrecognized exception condition "Question author and answer author shouldn't match!"
SQL state: 42704
Context: compilation of PL/pgSQL function "addanswer" near line 9

Appreciate any suggestions, thanks.

like image 892
Lazyexpert Avatar asked Sep 05 '25 21:09

Lazyexpert


1 Answers

You should not use " for stating the exception text. Use ' instead.

See https://www.postgresql.org/docs/9.3/static/plpgsql-errors-and-messages.html

like image 121
Uwe Allner Avatar answered Sep 10 '25 07:09

Uwe Allner