创建未终止的美元引用字符串的函数。

huangapple go评论95阅读模式
英文:

create function unterminated dollar-quoted string

问题

我正在尝试使用Goose和PostgreSQL(使用pq库)创建这个函数。

我的代码如下:

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上成功运行。

英文:

I'm trying to create this function with Goose using a postgres (pq lib) database.

My code is as follows:

   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;

When I try to goose up it provides an error:

(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 basically echo's the pq library error, so I dont think it's in Goose, but rather the pq-library. Query runs succesful on pgAdmin III.

答案1

得分: 27

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

你的语句中包含嵌入的分号,所以你需要使用这些注释。否则,goose会对SQL进行修改,导致libpq报错。

英文:

According to the goose documentation, complex statements that include semicolons must be annotated with -- +goose StatementBegin and -- +goose StatementEnd

Your statement contains semicolons embedded within it so you need to use these annotations. Otherwise goose mangles the SQL so that libpq gives errors.

huangapple
  • 本文由 发表于 2014年1月5日 22:11:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/20934544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定