@@ -89,12 +89,12 @@ defmodule AshSql.Aggregate do
8989 result =
9090 aggregates
9191 |> Enum . reject ( & already_added? ( & 1 , query . __ash_bindings__ , root_data_path ) )
92- |> Enum . group_by ( & { & 1 . relationship_path , & 1 . join_filters || % { } } )
93- |> Enum . flat_map ( fn { { path , join_filters } , aggregates } ->
92+ |> Enum . group_by ( & { & 1 . relationship_path , & 1 . join_filters || % { } , & 1 . query . action . name } )
93+ |> Enum . flat_map ( fn { { path , join_filters , read_action } , aggregates } ->
9494 { can_group , cant_group } =
9595 Enum . split_with ( aggregates , & can_group? ( resource , & 1 , query ) )
9696
97- [ { { path , join_filters } , can_group } ] ++
97+ [ { { path , join_filters , read_action } , can_group } ] ++
9898 Enum . map ( cant_group , & { { path , join_filters } , [ & 1 ] } )
9999 end )
100100 |> Enum . filter ( fn
@@ -106,8 +106,24 @@ defmodule AshSql.Aggregate do
106106 end )
107107 |> Enum . reduce_while (
108108 { :ok , query , [ ] } ,
109- fn { { [ first_relationship | relationship_path ] , join_filters } , aggregates } ,
109+ fn { { [ first_relationship | relationship_path ] = path , join_filters , read_action } ,
110+ aggregates } ,
110111 { :ok , query , dynamics } ->
112+ read_action =
113+ resource
114+ |> Ash.Resource.Info . related ( path )
115+ |> Ash.Resource.Info . action ( read_action )
116+
117+ if read_action . modify_query do
118+ raise """
119+ Data layer does not currently support aggregates over read actions that use `modify_query`.
120+
121+ Resource: #{ inspect ( resource ) }
122+ Relationship Path: #{ inspect ( path ) }
123+ Action: #{ read_action . name }
124+ """
125+ end
126+
111127 first_relationship =
112128 case Ash.Resource.Info . relationship ( resource , first_relationship ) do
113129 nil ->
0 commit comments