我正在尝试使用postgres(Goose)数据库使用pq lib创建此函数。
我的代码如下:
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上成功运行。
答案 0 :(得分:18)
根据goose文档,包含分号的复杂语句必须使用 - + goose StatementBegin和 - + goose StatementEnd
进行注释您的语句中包含嵌入分号,因此您需要使用这些注释。否则goose会破坏SQL,以便libpq出错。