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
Post a Comment