首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >长生不老经济学JSONb查询

长生不老经济学JSONb查询
EN

Stack Overflow用户
提问于 2017-09-26 03:10:46
回答 1查看 1.7K关注 0票数 1

我是Postgresql JSONb和Ecto的新手。我有一个列“配置”的表,即jsonb。我能够插入,但是当我尝试使用where条件从它中选择片段函数时,我无法让它工作。下面是示例和输出:

代码语言:javascript
复制
iex> Repo.all(from i in Instance, select: i.configuration, where: 
fragment("?->'testing' LIKE '%hey%'", i.configuration))

[debug] QUERY ERROR source="instances" db=0.4ms
SELECT i0."configuration" FROM "instances" AS i0 WHERE 
(i0."configuration"->'testing' LIKE '%hey%') []
** (Postgrex.Error) ERROR 42883 (undefined_function): operator does not 
exist: jsonb ~~ unknown
(ecto) lib/ecto/adapters/sql.ex:431: 
Ecto.Adapters.SQL.execute_and_cache/7
(ecto) lib/ecto/repo/queryable.ex:133: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4

如果我以这样的方式执行原始查询:

代码语言:javascript
复制
iex> query = """
select configuration FROM instances where configuration->>'testing' 
LIKE '%hey%'
"""
iex> Ecto.Adapters.SQL.query!(Repo, query)
[debug] QUERY OK db=1.0ms
select configuration FROM instances where configuration->>'testing' 
LIKE '%hey%' []
%Postgrex.Result{columns: ["configuration"], command: :select,
connection_id: 28581, num_rows: 1, rows: [[%{"testing" => "some test 
hey?"}]]}

同样,在psql中,以下查询工作:

代码语言:javascript
复制
select configuration FROM instances where configuration->>'tsting' LIKE '%hey%';

关于我在Repo.all上做错什么的任何帮助(.查询将不胜感激,因为我已经尝试了一堆,但没有效果,不明白我做错了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-26 03:27:15

第一个查询使用 operator,用于:

按键获取JSON对象字段

因此,它返回jsonb,错误告诉您,没有~~运算符(LIKE)在左侧接受jsonb

使用->>操作符(返回textLIKE (AKA ~~) )的查询确实知道如何处理左侧的text

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46417251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档