Crashes on non binary parameters passed to select queries
lukaszsamson opened this issue · 1 comments
lukaszsamson commented
Environment
Postgrex: master
ecto: 3.7.2
ecto_sql: 3.7.2
db_connection: 2.4.2
Description
Selects with non binary parameters like
Ecto.Adapters.SQL.query!(
Repo, "select $1", [1]
)
crash with
** (exit) an exception was raised:
** (DBConnection.EncodeError) Postgrex expected a binary, got 1. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
(postgrex 0.16.3) lib/postgrex/type_module.ex:947: Postgrex.DefaultTypes.encode_params/3
(postgrex 0.16.3) lib/postgrex/query.ex:75: DBConnection.Query.Postgrex.Query.encode/3
(db_connection 2.4.2) lib/db_connection.ex:1255: DBConnection.encode/5
(db_connection 2.4.2) lib/db_connection.ex:1355: DBConnection.run_prepare_execute/5
(db_connection 2.4.2) lib/db_connection.ex:1459: DBConnection.run/6
(db_connection 2.4.2) lib/db_connection.ex:595: DBConnection.parsed_prepare_execute/5
(db_connection 2.4.2) lib/db_connection.ex:587: DBConnection.prepare_execute/4
(postgrex 0.16.3) lib/postgrex.ex:351: Postgrex.query_prepare_execute/4
(ecto_sql 3.7.2) lib/ecto/adapters/sql.ex:367: Ecto.Adapters.SQL.query!/4
On the other hand this works
Ecto.Adapters.SQL.query!(
Repo, "select $1", ["abc"]
)
josevalim commented
This is correct, I believe. Postgrex does not perform automatic type conversion and by default it seems that "select $1" expects the argument to be a binary/string. You can pass a binary or cast the parameter to an integer. Something like:
Ecto.Adapters.SQL.query!(
Repo, "select $1::int", [1]
)