parent in an aggregate is the root resource, not the resource one up the aggregate relationship path. I would expect that to break more gracefully, but it would always break nonetheless.
Ah, thank you. That clarifies things.
Using the single parent now: filter expr(inserted_at == parent(last_read_message.inserted_at))
And got this:
** (Ash.Error.Unknown)
Bread Crumbs:
> Exception raised in: ConversationUser.read
Unknown Error
* ** (RuntimeError) Error while building reference: last_read_message.inserted_at
(ash_sql 0.2.59) lib/expr.ex:1915: AshSql.Expr.default_dynamic_expr/6
(ash_sql 0.2.59) lib/expr.ex:2868: AshSql.Expr.maybe_type_expr/6
(ash_sql 0.2.59) lib/expr.ex:1006: AshSql.Expr.default_dynamic_expr/6
(ash_sql 0.2.59) lib/filter.ex:37: anonymous fn/2 in AshSql.Filter.add_filter_expression/2
(elixir 1.17.2) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3
(ash_sql 0.2.59) lib/filter.ex:22: AshSql.Filter.filter/4
(ash_sql 0.2.59) lib/aggregate.ex:887: anonymous fn/6 in AshSql.Aggregate.maybe_filter_subquery/6
(elixir 1.17.2) lib/enum.ex:4858: Enumerable.List.reduce/3
(elixir 1.17.2) lib/enum.ex:2585: Enum.reduce_while/3
(ash_sql 0.2.59) lib/aggregate.ex:369: anonymous fn/10 in AshSql.Aggregate.add_aggregates/6
(ash_sql 0.2.59) lib/join.ex:290: AshSql.Join.related_subquery/3
(ash_sql 0.2.59) lib/aggregate.ex:217: anonymous fn/7 in AshSql.Aggregate.add_aggregates/6
(elixir 1.17.2) lib/enum.ex:4858: Enumerable.List.reduce/3
(elixir 1.17.2) lib/enum.ex:2585: Enum.reduce_while/3
(ash_sql 0.2.59) lib/aggregate.ex:88: AshSql.Aggregate.add_aggregates/6
(ash 3.4.66) lib/ash/query/query.ex:3157: Ash.Query.data_layer_query/2
(ash 3.4.66) lib/ash/actions/read/read.ex:595: anonymous fn/8 in Ash.Actions.Read.do_read/5
(ash 3.4.66) lib/ash/actions/read/read.ex:956: Ash.Actions.Read.maybe_in_transaction/3
(ash 3.4.66) lib/ash/actions/read/read.ex:315: Ash.Actions.Read.do_run/3
(ash 3.4.66) lib/ash/actions/read/read.ex:82: anonymous fn/3 in Ash.Actions.Read.run/3
defmodule ConversationUser do
relationships do
belongs_to :last_read_message, Message,
source_attribute: :last_read_message_id,
public?: true
end
aggregates do
count :unread_messages_count2, [:conversation, :messages] do
filter expr(inserted_at == parent(last_read_message.inserted_at))
end
end
Ash.get!(ConversationUser, id,
authorize?: false,
load: [:unread_messages_count2]
)