elixir - Ecto "left IN right" query using a fragment -


i query jsonb field using postgres in operator (with ecto library)

this code work simple = operator:

from in query, where: fragment("?->>'format' = ?", a.properties, "foo") 

but cannot make of these attempts work:

from in query, where: fragment("?->>'format' in ?", a.properties, ["foo", "bar"]) in query, where: fragment("?->>'format' in (?)", a.properties, ["foo", "bar"]) in query, where: fragment("?->>'format' in ?", a.properties, "('foo', 'bar')"]) 

any idea?

besides patrick's excellent response, keep in mind can put part of query in fragment too. example, can rewrite to:

from in query, where: fragment("?->>'format', a.properties) in ["foo", "bar"] 

if put fragment in macro, can readable syntax:

defmacro jsonb_get(left, right)   quote     fragment("?->>?", unquote(left), unquote(right))   end end 

and now:

from in query, where: jsonb_get(a.properties, "format") in ["foo", "bar"] 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -