0

I'm getting a syntax error but having issues identifying what exactly the issue is. I'm not sure if I'm misunderstanding things here -- but it looks like the INSERT INTO is breaking, but only when it's in the function.

I'm running the below through Hasura:

CREATE OR REPLACE FUNCTION custom_q_into_selected()  
    RETURNS trigger AS $BODY$ 
    BEGIN
    IF NEW.is_custom = true THEN 
        INSERT INTO selected_question(book_id,question_id) 
        VALUES(NEW.custom_for_book_id,NEW.id);
    END IF;
    RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;

Keep getting this error when I run this:

postgres-error: syntax error at or near "\"

Database logs:

CREATE OR REPLACE FUNCTION custom_q_into_selected() \r

RETURNS trigger AS $BODY$\r

BEGIN\r

IF NEW.is_custom THEN \r

    INSERT INTO selected_question(book_id,question_id) VALUES(NEW.custom_for_book_id,NEW.id);\r

END IF;\r

RETURN NEW;\r

END;\r

$BODY$ LANGUAGE plpgsql;\r

Any help is appreciated.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
doh
  • 21
  • 5
  • 3
    There is no `\ ` in your code. Please check what you're running again. Also, `IF NEW.is_custom = true THEN` can be simplified to just `IF NEW.is_custom THEN` – Bohemian Sep 12 '21 at 22:50
  • How are you running the `CREATE` command? Make sure nothing is escaping the body string. Check the database logs to see the full statement that was executed. – Bergi Sep 12 '21 at 22:55
  • I'm running this in Hasura, and I've literally copied and pasted my SQL code. Will edit the above with the database logs. @Bergi – doh Sep 12 '21 at 23:29
  • 1
    Looks like those `\r` are the culprit… – Bergi Sep 12 '21 at 23:34
  • omg it worked lol @Bergi... – doh Sep 12 '21 at 23:35
  • Those `\r` are obviously supposed to be [carriage return characters](https://en.wikipedia.org/wiki/Carriage_return). It's been a while since I've seen those misinterpreted. It's the old struggle between Windows, Linux and Mac having different encodinges for newline. – Erwin Brandstetter Sep 12 '21 at 23:35
  • 1
    Bill Gates got rich on `\r\n`, so it must be a feature, not a bug. – wildplasser Sep 12 '21 at 23:40
  • 1
    @wildplasser Don't credit Microsoft for creativity. They just ripped off CP/M. – Laurenz Albe Sep 13 '21 at 02:23

0 Answers0