英文:
create function unterminated dollar-quoted string
问题
我正在尝试使用Goose和PostgreSQL(使用pq库)创建这个函数。
我的代码如下:
CREATE OR REPLACE FUNCTION add_userlocation(user_id INT, location_id INT) RETURNS VOID AS
$BODY$
BEGIN
LOOP
UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id;
IF found THEN
RETURN;
END IF;
BEGIN
INSERT INTO userslocations(userid,locationid, count) VALUES (user_id, location_id, 1);
RETURN;
EXCEPTION WHEN unique_violation THEN
END;
END LOOP;
END;
$BODY$
LANGUAGE plpgsql;
当我尝试运行goose up
时,它报错:
(pq: unterminated dollar-quoted string at or near "$BODY$
BEGIN
LOOP
-- first try to update the key
UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id;
"), quitting migration.
Goose基本上会输出pq
库的错误,所以我认为问题不在于Goose,而是pq库。该查询在pgAdmin III上成功运行。
英文:
I'm trying to create this function with Goose using a postgres (pq lib) database.
My code is as follows:
CREATE OR REPLACE FUNCTION add_userlocation(user_id INT, location_id INT) RETURNS VOID AS
$BODY$
BEGIN
LOOP
UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id;
IF found THEN
RETURN;
END IF;
BEGIN
INSERT INTO userslocations(userid,locationid, count) VALUES (user_id, location_id, 1);
RETURN;
EXCEPTION WHEN unique_violation THEN
END;
END LOOP;
END;
$BODY$
LANGUAGE plpgsql;
When I try to goose up
it provides an error:
(pq: unterminated dollar-quoted string at or near "$BODY$
BEGIN
LOOP
-- first try to update the key
UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id;
"), quitting migration.
Goose basically echo's the pq
library error, so I dont think it's in Goose, but rather the pq-library. Query runs succesful on pgAdmin III.
答案1
得分: 27
根据goose文档的说明,包含分号的复杂语句必须使用-- +goose StatementBegin
和-- +goose StatementEnd
进行注释。
你的语句中包含嵌入的分号,所以你需要使用这些注释。否则,goose会对SQL进行修改,导致libpq报错。
英文:
According to the goose documentation, complex statements that include semicolons must be annotated with -- +goose StatementBegin
and -- +goose StatementEnd
Your statement contains semicolons embedded within it so you need to use these annotations. Otherwise goose mangles the SQL so that libpq gives errors.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论