create function unterminated dollar-quoted string

pur*_*ion 9 postgresql go libpq goose

我正在尝试使用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;
Run Code Online (Sandbox Code Playgroud)

当我尝试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.
Run Code Online (Sandbox Code Playgroud)

Goose基本上回应了pq库错误,所以我不认为它在Goose中,而是pq库.查询在pgAdmin III上成功运行.

har*_*mic 22

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

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