create function unterminated dollar-quoted string

时间:2014-01-05 14:11:02

标签: postgresql go libpq goose

我正在尝试使用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上成功运行。

1 个答案:

答案 0 :(得分:18)

根据goose文档,包含分号的复杂语句必须使用 - + goose StatementBegin和 - + goose StatementEnd

进行注释

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