Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load aggregate if the loaded relationship loads another relationship #806

Closed
sezaru opened this issue Dec 17, 2023 · 0 comments
Closed
Labels
bug Something isn't working needs review

Comments

@sezaru
Copy link
Contributor

sezaru commented Dec 17, 2023

Describe the bug
If the aggregated resource has a default preparation that will load other resources, the aggregation will fail

To Reproduce
First we have a resource with these aggregations and relationships:

  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

And in the Class resource, we have the following preparations:

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

Now, if we try to load :total_classes we will 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
 }}

Expected behavior
The aggregation should work and the preparations of the other resource should be ignored.

Runtime

  • Elixir version 1.15
  • Erlang version 26
  • OS Fedora 39
  • Ash version 2.17.13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs review
Projects
None yet
Development

No branches or pull requests

1 participant