Home >Backend Development >Golang >How to Resolve the Unterminated Dollar-Quoted String Error When Creating PostgreSQL Functions in Goose?
Unterminated Dollar-Quoted String Error in Goose: Resolving PostgreSQL Function Creation Issues
When attempting to create a function in PostgreSQL using Goose, you may encounter an error related to an unterminated dollar-quoted string. This error is typically triggered when the SQL statement the function contains semicolons, which break up the string.
To resolve this issue, Goose users must annotate these complex statements with -- goose StatementBegin and -- goose StatementEnd. These annotations separate the statement into smaller chunks, allowing Goose to process it correctly.
In your specific case, the code contains semicolons in the UPDATE and INSERT statements. Therefore, the annotated code should appear as follows:
CREATE OR REPLACE FUNCTION add_userlocation(user_id INT, location_id INT) RETURNS VOID AS $BODY$ -- +goose StatementBegin BEGIN LOOP UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id; -- +goose StatementEnd IF found THEN RETURN; END IF; -- +goose StatementBegin BEGIN INSERT INTO userslocations(userid,locationid, count) VALUES (user_id, location_id, 1); RETURN; EXCEPTION WHEN unique_violation THEN -- +goose StatementEnd END; END LOOP; END; $BODY$ LANGUAGE plpgsql;
Once these annotations have been added, Goose will be able to properly process the SQL statement and create the function without encountering the unterminated string error. Remember, if your statements contain embedded semicolons, it is crucial to add these annotations to ensure compatibility with Goose and the pq library.
The above is the detailed content of How to Resolve the Unterminated Dollar-Quoted String Error When Creating PostgreSQL Functions in Goose?. For more information, please follow other related articles on the PHP Chinese website!