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

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

create function unterminated dollar-quoted string

问题

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

我的代码如下:

  1. CREATE OR REPLACE FUNCTION add_userlocation(user_id INT, location_id INT) RETURNS VOID AS
  2. $BODY$
  3. BEGIN
  4. LOOP
  5. UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id;
  6. IF found THEN
  7. RETURN;
  8. END IF;
  9. BEGIN
  10. INSERT INTO userslocations(userid,locationid, count) VALUES (user_id, location_id, 1);
  11. RETURN;
  12. EXCEPTION WHEN unique_violation THEN
  13. END;
  14. END LOOP;
  15. END;
  16. $BODY$
  17. LANGUAGE plpgsql;

当我尝试运行goose up时,它报错:

  1. (pq: unterminated dollar-quoted string at or near "$BODY$
  2. BEGIN
  3. LOOP
  4. -- first try to update the key
  5. UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id;
  6. "), 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:

  1. CREATE OR REPLACE FUNCTION add_userlocation(user_id INT, location_id INT) RETURNS VOID AS
  2. $BODY$
  3. BEGIN
  4. LOOP
  5. UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id;
  6. IF found THEN
  7. RETURN;
  8. END IF;
  9. BEGIN
  10. INSERT INTO userslocations(userid,locationid, count) VALUES (user_id, location_id, 1);
  11. RETURN;
  12. EXCEPTION WHEN unique_violation THEN
  13. END;
  14. END LOOP;
  15. END;
  16. $BODY$
  17. LANGUAGE plpgsql;

When I try to goose up it provides an error:

  1. (pq: unterminated dollar-quoted string at or near "$BODY$
  2. BEGIN
  3. LOOP
  4. -- first try to update the key
  5. UPDATE userslocations SET count = count+1 WHERE userid = user_id AND locationid = location_id;
  6. "), 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:

确定