Hi all,
I have a table books
with columns name
and version
. I want to search for books matching one of the provided pairs of (name, version). The SQL query looks like this:
select * from public.books
where (name, version) in (('ruby', 'v1'), ('ruby', 'v2'), ('elixir', 'v1'))
What I have tried:
signatures = [{"ruby", "v1"}, {"ruby", "v2"}, {"elixir", "v1"}]
from(b in Book,
where: fragment("(?, ?) IN (?)", b.name, b.version, ^signatures)
)
This fails with:
** (DBConnection.EncodeError) Postgrex expected a tuple, got [{“ruby”, “v1”}, {“ruby”, “v2”}, {“elixir”, “v1”}]. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
Well, I tried a few more options, but they made even less sense. I feel like such a simple expression should be possible to express in Ecto?
(I am not even sure how this constructions (col1, col2, …) are called in SQL but I suspect this is row constructors, because I can replace my query with select * from public.books where ROW(name, version) in (ROW('ruby', '1'), ROW(...), ...)
) and it works the same.
Thanks!