Ecto SQL 片段失败,为什么?
Ecto SQL Fragment fails, why?
正在尝试执行 SQL 片段:
Repo.all from p in Posts, where: fragment("lower(?) in ?", p.title, ^["some-title"])
但它失败了,它生成以下 SQL 和错误:
SELECT p0."title" FROM "posts" AS p0 WHERE (lower(p0."title") in ) [["some-title"]]
** (Postgrex.Error) ERROR (syntax_error): syntax error at or near ""
更新:解决方案
所以,经过大量的尝试,我想出了如何使用它:
Repo.all from p in Posts, where: fragment("lower(?)", p.title) in ^["some-title"])
但仍然 - 为什么原来的表达式不起作用?好像也完全正确。
更新
There should be parentheses after in
我试过了,也没用:
Repo.all from p in Posts, where: fragment("lower(?) in (?)", p.title, ^["some-title"])
** (ArgumentError) Postgrex expected a binary that can be encoded/cast to type "text", got ["some-title"]. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
1) 是的,正确的做法是:
Repo.all from p in Posts, where: fragment("lower(?)", p.title) in ["some-title"])
2) 原始查询不起作用,因为它生成了错误的 SQL 语法。
它生成如下内容:
... where lower(p.title) in ["some-title"] ...
正确的语法是:
... where lower(p.title) in ('some-title') ...
正在尝试执行 SQL 片段:
Repo.all from p in Posts, where: fragment("lower(?) in ?", p.title, ^["some-title"])
但它失败了,它生成以下 SQL 和错误:
SELECT p0."title" FROM "posts" AS p0 WHERE (lower(p0."title") in ) [["some-title"]]
** (Postgrex.Error) ERROR (syntax_error): syntax error at or near ""
更新:解决方案
所以,经过大量的尝试,我想出了如何使用它:
Repo.all from p in Posts, where: fragment("lower(?)", p.title) in ^["some-title"])
但仍然 - 为什么原来的表达式不起作用?好像也完全正确。
更新
There should be parentheses after
in
我试过了,也没用:
Repo.all from p in Posts, where: fragment("lower(?) in (?)", p.title, ^["some-title"])
** (ArgumentError) Postgrex expected a binary that can be encoded/cast to type "text", got ["some-title"]. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
1) 是的,正确的做法是:
Repo.all from p in Posts, where: fragment("lower(?)", p.title) in ["some-title"])
2) 原始查询不起作用,因为它生成了错误的 SQL 语法。 它生成如下内容:
... where lower(p.title) in ["some-title"] ...
正确的语法是:
... where lower(p.title) in ('some-title') ...