ash-project / ash_sql

A library containing some shared Ecto-based sql data layer functionality.
MIT License
4 stars 8 forks source link

List aggregate on nested resource's calculation raise error #50

Closed mars22 closed 3 months ago

mars22 commented 3 months ago

Describe the bug On top level List resource i would like to aggregate data from deep nested User resource.

  defmodule List do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer

   aggregates do
      list(:members, [:resources, :party, :party_members, :user], :profile)
   end
  end

  defmodule User do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer,

    calculations do
        calculate :profile, :map, expr(%{name: name, email: email})
    end
  end

When fetching data using

Ash.read(List, load: :members)

I've get this error

The following arguments were given to anonymous fn/2 in AshSql.Expr.default_dynamic_expr/6:

         # 1
         {:email, party.party_members.user.email}

         # 2
         nil

          stacktrace:
       (ash_sql 0.2.16) lib/expr.ex:1919: anonymous fn/2 in AshSql.Expr.default_dynamic_expr/6
       (stdlib 6.0) maps.erl:860: :maps.fold_1/4
       (ash_sql 0.2.16) lib/expr.ex:1919: AshSql.Expr.default_dynamic_expr/6
       (ash_sql 0.2.16) lib/aggregate.ex:1372: AshSql.Aggregate.add_subquery_aggregate_select/6
       (elixir 1.17.1) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ash_sql 0.2.16) lib/join.ex:276: AshSql.Join.related_subquery/3
       (ash_sql 0.2.16) lib/aggregate.ex:159: anonymous fn/7 in AshSql.Aggregate.add_aggregates/6
       (elixir 1.17.1) lib/enum.ex:4858: Enumerable.List.reduce/3
       (elixir 1.17.1) lib/enum.ex:2585: Enum.reduce_while/3
       (ash_sql 0.2.16) lib/aggregate.ex:88: AshSql.Aggregate.add_aggregates/6
       (ash 3.1.5) lib/ash/query/query.ex:2963: Ash.Query.data_layer_query/2
       (ash 3.1.5) lib/ash/actions/read/read.ex:446: anonymous fn/5 in Ash.Actions.Read.do_read/4
       (ash 3.1.5) lib/ash/actions/read/read.ex:786: Ash.Actions.Read.maybe_in_transaction/3
       (ash 3.1.5) lib/ash/actions/read/read.ex:249: Ash.Actions.Read.do_run/3
       (ash 3.1.5) lib/ash/actions/read/read.ex:66: anonymous fn/3 in Ash.Actions.Read.run/3
       (ash 3.1.5) lib/ash/actions/read/read.ex:65: Ash.Actions.Read.run/3
       (ash 3.1.5) lib/ash.ex:1855: Ash.read/2
       (ash 3.1.5) lib/ash.ex:1814: Ash.read!/2

Data relations looks as follow List - has_many -> Resources -belongs_to -> Party -has_many -> PartyMembers -belongs_to -> User

Expected behavior List of List resources with members data like:

%List{
members: [%{email: ..., name: ....}]
}

** Runtime

zachdaniel commented 3 months ago

Fixed in v0.2.22