2017-09-26 63 views
0

我是Postgresql JSONb和Ecto的新手。我有一個列「配置」,是jsonb的表。我能夠插入,但是當我嘗試從中選擇使用片段函數的條件時,我無法使其工作。下面是示例輸出:Elixir Ecto JSONb查詢

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 

如果我執行原始查詢這樣:

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以下查詢工作:

select configuration FROM instances where configuration->>'tsting' LIKE '%hey%'; 

任何幫助的我正在做什麼錯誤的Repo.all(...查詢將不勝感激,因爲我已經嘗試了一堆無濟於事,不明白我做錯了什麼。

回答

1

你的第一個查詢使用-> operator和用於:通過關鍵

獲取JSON對象字段,以便它給你jsonb背部和錯誤告訴你,有沒有~~運營商( AKA LIKE),在左側需要jsonb

的作品查詢使用->>操作返回textLIKE(AKA ~~)不知道該怎麼左側與text做。

+0

一路走來,我有 - - 在某些時候,可能還有一些其他錯誤,除了沒有閱讀所有的文檔和了解 - >操作符。我很欣賞清晰度。 – bexley

+0

關於'~~'而不是'LIKE'的錯誤信息會引起混淆,除非您已經知道'~~'和'LIKE'是相同的東西。別客氣。 –