Ecto: Dynamic DynamicExpr doesn't work as expected

Created on 1 Feb 2017  路  3Comments  路  Source: elixir-ecto/ecto

Environment

  • Elixir version (elixir -v): Erlang/OTP 19 [erts-8.2] Elixir 1.4.0
  • Database and version: Postgres (PostgreSQL) 9.6.1
  • Ecto version (mix deps): 2.1.2 or 2.1.3
  • Database adapter and version (mix deps): postgrex 0.13.0
  • Operating system: macOS Sierra 10.12.2

Current behavior

First issue - Inspect fails here for some reason

conds = dynamic([o, b], b.user_id == ^1 or ^false)
dynamic = dynamic([o], o.type == ^2 and ^conds)

IO.inspect dynamic

%Inspect.Error{message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: Ecto.Query.DynamicExpr, binding: [{:o, [line: 7], nil}], file: \"/.../test/ecto_dynamicexpr_test.exs\", fun: #Function<1.104152044/1 in Ecto.DynamicExprTest.test dynamic expression builds/1>, line: 7}"}

Second issue - Dynamically created where passes arguments to the prepared statement in incorrect order (please note I passed conds as the first part of the second dynamic statement. In the first issue running this query was succesful actually)

conds = dynamic([o, b], b.user_id == ^1 or ^false)
dynamic = dynamic([o], ^conds and o.type == ^2)

IO.inspect dynamic

(from o in Order,
join: b in assoc(o, :balance),
where: ^dynamic)
|> Repo.all
|> IO.inspect


%Inspect.Error{message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: Ecto.Query.DynamicExpr, binding: [{:o, [line: 7], nil}], file: \"/Users/kelu/Dropbox/Projects/binaryoptions/test/ecto_dynamicexpr_test.exs\", fun: #Function<1.8868631/1 in Ecto.DynamicExprTest.test dynamic expression builds/1>, line: 7}"}
13:40:20.819 [debug] QUERY ERROR source="orders" db=4.1ms
SELECT o0."id", o0."symbol", o0."type", o0."value", o0."open_time", o0."close_time", o0."open_price", o0."close_price", o0."payout", o0."profit", o0."comment", o0."balance_id", o0."inserted_at", o0."updated_at" FROM "orders" AS o0 INNER JOIN "balances" AS b1 ON b1."id" = o0."balance_id" WHERE (((b1."user_id" = $1) OR $2) AND (o0."type" = $3)) [2, 1, false]


** (ArgumentError) Postgrex expected a boolean, 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.
     stacktrace:
       (ecto) /Users/kelu/Dropbox/Projects/binaryoptions/deps/postgrex/lib/postgrex/type_module.ex:716: Ecto.Adapters.Postgres.TypeModule.encode_params/3
       (postgrex) lib/postgrex/query.ex:45: DBConnection.Query.Postgrex.Query.encode/3
       (db_connection) lib/db_connection.ex:1070: DBConnection.describe_run/5
       (db_connection) lib/db_connection.ex:1141: anonymous fn/4 in DBConnection.run_meter/5
       (db_connection) lib/db_connection.ex:1198: DBConnection.run_begin/3
       (db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4
       (ecto) lib/ecto/adapters/postgres/connection.ex:80: Ecto.Adapters.Postgres.Connection.prepare_execute/5
       (ecto) lib/ecto/adapters/sql.ex:243: Ecto.Adapters.SQL.sql_call/6
       (ecto) lib/ecto/adapters/sql.ex:431: Ecto.Adapters.SQL.execute_and_cache/7
       (ecto) lib/ecto/repo/queryable.ex:130: Ecto.Repo.Queryable.execute/5
       (ecto) lib/ecto/repo/queryable.ex:35: Ecto.Repo.Queryable.all/4
       test/ecto_dynamicexpr_test.exs:14: (test)
Bug Advanced

Most helpful comment

Thank you for the report. I have pushed fixes to the v2.1 branch (master as well). Can you please give it a try? If it works I will ship a new version. Thank you!

All 3 comments

You can pull and check what I mean here: https://github.com/kelostrada/ecto_dynamicexpr_bug

Thank you for the report. I have pushed fixes to the v2.1 branch (master as well). Can you please give it a try? If it works I will ship a new version. Thank you!

It's fixed <3

Build Status

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brandonparsons picture brandonparsons  路  3Comments

nathanjohnson320 picture nathanjohnson320  路  4Comments

yordis picture yordis  路  4Comments

ericmj picture ericmj  路  3Comments

mcginleyr1 picture mcginleyr1  路  5Comments