【发布时间】:2014-01-05 14:11:02
【问题描述】:
我正在尝试使用 postgres (pq lib) 数据库使用 Goose 创建此函数。
我的代码如下:
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-library 中。查询在 pgAdmin III 上运行成功。
【问题讨论】:
-
如果将函数定义直接粘贴到psql中,可以吗?
-
是的,我在 pgAdmin III 中使用了查询窗口,我在其中粘贴了代码并执行了它。它有效,我可以将它与我的 go-app/pq 库一起使用 (
exec("Select add_userlocation(1,1)")) -
这意味着 goose 必须在将文本传递给 libpq 之前对其进行修改。它对输入字符串做任何解释吗?
标签: postgresql go libpq goose