Postgrex WHERE in ($1)

What is the correct syntax to perform a raw query with WHERE IN ($1) condition?

I thought this would work, but I get an error

Ecto.Adapters.SQL.query!(App.Repo, "SELECT category_id from category_statistics where id in ($1)", [[1,2,3]])

** (DBConnection.EncodeError) Postgrex expected an integer in -9223372036854775808..9223372036854775807, got [1, 2, 3]. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.

Have a look at this closed issue on github with the following solution. Its more of a postgres syntax

Postgrex.query!(conn, "SELECT * FROM #{table_name} WHERE id = ANY($1)", [[1,2]])

This worked. Thanks!

1 Like