Selecting a pinned value gives an error

In SQL you can write: SELECT 1 as some_value; which gives you

some_value 
------------
          1

I’m try in to add a value to a query using ecto like this:

query |> select([x], %{x | some_value: ^value})

which gives me the following error:

     ** (DBConnection.EncodeError) Postgrex expected a binary, got 4. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.

When I just enter the 4 it works:

query |> select([x], %{x | some_value: 4})

Can someone explain what the problem is and help me with a workaround?

Thanks!

Postgrex assumes all parameters (i.e. interpolated with pin) are binaries by default. This should fix it:

query |> select([x], %{x | some_value: type(^value, :integer)})
2 Likes