没有括号的外片段
Ecto fragment without parentheses
我的 Postgres table 结构:
id | stuff
--------+------------------------------------------------------------
123 | {"type1": {"ref": "ref_1", "...": "..."}, "type2": {"ref": "ref_1", "...": "..."}}
我想通过 ref
查询每种类型的东西,我有一个有效的 SQL 查询:
SELECT * FROM "stuff" AS c0 CROSS JOIN jsonb_each(c0."stuff") AS f1 WHERE value->>'ref' = 'ref_1';
但是使用这个 Ecto 查询:
(from c in Stuff,
join: fragment("jsonb_each(?)", c.stuff),
where: fragment("value->>'ref' = ?", ^ref)
)
|> Repo.all
我在 CROSS JOIN 语句中遇到 Postgres 语法错误:
** (Postgrex.Error) ERROR 42601 (syntax_error): syntax error at or near ")"
检查生成的查询:
[debug] QUERY ERROR source="stuff" db=0.3ms
SELECT ... FROM "stuff" AS c0 CROSS JOIN (jsonb_each(c0."stuff")) AS f1 WHERE (value->>'ref' = ) ["ref_1"]
当我删除 (jsonb_each(c0."stuff"))
.
周围的外括号时,上面的方法有效
有没有办法让片段生成不带这些括号的查询,或者我是否必须重新设计查询?
谢谢
Ecto 似乎总是将 join 子句括在括号中,这通常没问题。不幸的是,它不包括某些调用的时间,如上面的 jsonb_each。对于这种情况,这里有一个 wiki:The parentheses rules of PostgreSQL, is there a summarized guide?
链接的原始 sql 示例有一个不太受欢迎的答案,似乎在进行此查询和返回预期结构方面都很好。
sql = "SELECT * FROM "stuff" AS c0 CROSS JOIN jsonb_each(c0."stuff") AS f1 WHERE value->>'ref' = 'ref_1';"
result = JsonbTest.Repo.query!(sql)
Enum.map(result.rows, &JsonbTest.Repo.load(StuffStruct, {result.columns, &1}))
这是 ecto 中的一个错误,已在此处修复https://github.com/elixir-ecto/ecto/issues/2537
我的 Postgres table 结构:
id | stuff
--------+------------------------------------------------------------
123 | {"type1": {"ref": "ref_1", "...": "..."}, "type2": {"ref": "ref_1", "...": "..."}}
我想通过 ref
查询每种类型的东西,我有一个有效的 SQL 查询:
SELECT * FROM "stuff" AS c0 CROSS JOIN jsonb_each(c0."stuff") AS f1 WHERE value->>'ref' = 'ref_1';
但是使用这个 Ecto 查询:
(from c in Stuff,
join: fragment("jsonb_each(?)", c.stuff),
where: fragment("value->>'ref' = ?", ^ref)
)
|> Repo.all
我在 CROSS JOIN 语句中遇到 Postgres 语法错误:
** (Postgrex.Error) ERROR 42601 (syntax_error): syntax error at or near ")"
检查生成的查询:
[debug] QUERY ERROR source="stuff" db=0.3ms
SELECT ... FROM "stuff" AS c0 CROSS JOIN (jsonb_each(c0."stuff")) AS f1 WHERE (value->>'ref' = ) ["ref_1"]
当我删除 (jsonb_each(c0."stuff"))
.
有没有办法让片段生成不带这些括号的查询,或者我是否必须重新设计查询?
谢谢
Ecto 似乎总是将 join 子句括在括号中,这通常没问题。不幸的是,它不包括某些调用的时间,如上面的 jsonb_each。对于这种情况,这里有一个 wiki:The parentheses rules of PostgreSQL, is there a summarized guide?
链接的原始 sql 示例有一个不太受欢迎的答案,似乎在进行此查询和返回预期结构方面都很好。
sql = "SELECT * FROM "stuff" AS c0 CROSS JOIN jsonb_each(c0."stuff") AS f1 WHERE value->>'ref' = 'ref_1';"
result = JsonbTest.Repo.query!(sql)
Enum.map(result.rows, &JsonbTest.Repo.load(StuffStruct, {result.columns, &1}))
这是 ecto 中的一个错误,已在此处修复https://github.com/elixir-ecto/ecto/issues/2537