【问题标题】:create function unterminated dollar-quoted string创建函数未终止的美元引号字符串
【发布时间】: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


【解决方案1】:

根据goose documentation,包含分号的复杂语句必须使用-- +goose StatementBegin-- +goose StatementEnd进行注释

您的语句包含嵌入其中的分号,因此您需要使用这些注释。否则 goose 会破坏 SQL,从而导致 libpq 出错。

【讨论】:

  • 你的回答救了我 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 2013-12-10
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 2016-06-14
相关资源
最近更新 更多