Cannot load aggregate if the loaded relationship loads another relationship

I have the following aggregates and relationships in my resource:

  aggregates do
    count :total_students, :students
    count :total_classes, :classes
  end

  relationships do
    alias Core.Feedbacks

    has_many :classes, Feedbacks.Class
    has_many :students, Feedbacks.Student
  end

When I try to load total_students it works just fine. But when I try to load total_classes I get the following error:

iex(18)> Core.Feedbacks.read!(Core.Feedbacks.School) |> List.first |> Core.Feedbacks.load([:total_classes])
[debug] QUERY OK source="schools" db=0.3ms idle=795.8ms
SELECT s0."id", s0."name", s0."inserted_at", s0."updated_at", s0."organization_id" FROM "schools" AS s0 []
{:error,
 %Ash.Error.Unknown{
   errors: [
     %Ash.Error.Unknown.UnknownError{
       error: "Cannot load in an aggregate.",
       field: nil,
       changeset: nil,
       query: nil,
       error_context: ["Loading aggregate: :total_classes for query: #Ash.Query<resource: Core.Feedbacks.School>"],
       vars: [],
       path: [:aggregates],
       stacktrace: #Stacktrace<>,
       class: :unknown
     }
   ],
   stacktraces?: true,
   changeset: nil,
   query: nil,
   error_context: [],
   vars: [],
   path: [],
   stacktrace: #Stacktrace<>,
   class: :unknown
 }}

In my Class resource, I have the following preparations:

  preparations do
    prepare build(load: [:students])
  end

If I remove this, then the aggregation works fine.

Hm…yeah we should just ignore the load statement, instead of showing an error. Can you open an issue for this on github? I’ll switch it to that behavior :slight_smile:

done Cannot load aggregate if the loaded relationship loads another relationship · Issue #806 · ash-project/ash · GitHub

Thanks, will fix tomorrow :slight_smile:

1 Like