Error about missing actor when referencing an aggregate in a calculation

I have 2 resources: X.Core.Project (api: X.Core) and X.Core.Call (api: X.Core)

Project has_many :calls
Project has a count :count_calls, :calls
Project has a calculation calculate :why_you_no_work, :boolean, expr(count_calls == 0)

X.Core has

  authorization do
    authorize :by_default
    require_actor? true
  end

this works:

Ash.get!(X.Core.Project, "some-id", actor: actor) |> Ash.load!([:count_calls], actor: actor)

and this fails:

Ash.get!(X.Core.Project, "some-id", actor: actor) |> Ash.load!([:count_calls, :why_you_no_work], actor: actor)

with the following stacktrace:

** (Ash.Error.Forbidden) Forbidden

* The api X.Core requires that an actor is provided at all times and none was provided.
  (elixir 1.16.2) lib/process.ex:860: Process.info/2
  (ash 2.21.12) lib/ash/error/exception.ex:59: Ash.Error.Forbidden.ApiRequiresActor.exception/1
  (ash 2.21.12) lib/ash/actions/helpers.ex:167: Ash.Actions.Helpers.add_actor/3
  (ash 2.21.12) lib/ash/actions/helpers.ex:110: Ash.Actions.Helpers.set_opts/3
  (ash 2.21.12) lib/ash/actions/helpers.ex:101: Ash.Actions.Helpers.add_process_context/3
  (ash 2.21.12) lib/ash/query/query.ex:462: Ash.Query.for_read/4
  (ash 2.21.12) lib/ash/filter/filter.ex:3550: Ash.Filter.do_hydrate_refs/2
  (ash 2.21.12) lib/ash/filter/filter.ex:3715: anonymous fn/3 in Ash.Filter.do_hydrate_refs/2
  (elixir 1.16.2) lib/enum.ex:4839: Enumerable.List.reduce/3
  (elixir 1.16.2) lib/enum.ex:2582: Enum.reduce_while/3
  (ash 2.21.12) lib/ash/filter/filter.ex:3714: Ash.Filter.do_hydrate_refs/2
  (ash 2.21.12) lib/ash/filter/filter.ex:3223: Ash.Filter.resolve_call/2
  (ash 2.21.12) lib/ash/resource/calculation/expression.ex:107: Ash.Resource.Calculation.Expression.select/3
  (ash 2.21.12) lib/ash/query/query.ex:1331: Ash.Query.select_and_load_calc/3
  (ash 2.21.12) lib/ash/query/query.ex:1285: Ash.Query.load_resource_calculation/4
  (elixir 1.16.2) lib/enum.ex:2528: Enum."-reduce/3-lists^foldl/2-0-"/3
  (ash 2.21.12) lib/ash/api/api.ex:2480: Ash.Api.load/4
  (ash 2.21.12) lib/ash/api/api.ex:2460: Ash.Api.load/4
  (ash 2.21.12) lib/ash/api/api.ex:2429: Ash.Api.load!/4
  (elixir 1.16.2) elixir.erl:405: :elixir.eval_external_handler/3
    (ash 2.21.12) lib/ash/actions/helpers.ex:167: Ash.Actions.Helpers.add_actor/3
    (ash 2.21.12) lib/ash/actions/helpers.ex:110: Ash.Actions.Helpers.set_opts/3
    (ash 2.21.12) lib/ash/actions/helpers.ex:101: Ash.Actions.Helpers.add_process_context/3
    (ash 2.21.12) lib/ash/query/query.ex:462: Ash.Query.for_read/4
    (ash 2.21.12) lib/ash/filter/filter.ex:3550: Ash.Filter.do_hydrate_refs/2
    (ash 2.21.12) lib/ash/filter/filter.ex:3715: anonymous fn/3 in Ash.Filter.do_hydrate_refs/2
    (elixir 1.16.2) lib/enum.ex:4839: Enumerable.List.reduce/3
    (elixir 1.16.2) lib/enum.ex:2582: Enum.reduce_while/3
    (ash 2.21.12) lib/ash/filter/filter.ex:3714: Ash.Filter.do_hydrate_refs/2
    (ash 2.21.12) lib/ash/filter/filter.ex:3223: Ash.Filter.resolve_call/2
    (ash 2.21.12) lib/ash/resource/calculation/expression.ex:107: Ash.Resource.Calculation.Expression.select/3
    (ash 2.21.12) lib/ash/query/query.ex:1331: Ash.Query.select_and_load_calc/3
    (ash 2.21.12) lib/ash/query/query.ex:1285: Ash.Query.load_resource_calculation/4
    (elixir 1.16.2) lib/enum.ex:2528: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ash 2.21.12) lib/ash/api/api.ex:2480: Ash.Api.load/4
    (ash 2.21.12) lib/ash/api/api.ex:2460: Ash.Api.load/4
    (ash 2.21.12) lib/ash/api/api.ex:2429: Ash.Api.load!/4
    iex:12: (file)

is this a bug or am I missing something?

1 Like

This is definitely an internal bug. Likely a result of us not setting an “internal” flag somewhere. Please open a GitHub issue on ash core with these details and I’ll look into it shortly.

I believe I’ve fixed this in main. Please try it out and let me know.

I looks like ash/main is the 3.0 branch, so that makes it non trivial to test in our project. (We plan to upgrade, but that will take a bit longer)

I’ve pushed an equivalent change to the 2.0 branch, so you could try that if you like :slight_smile:

@zachdaniel yup, that worked, thank you!

1 Like

The fix is now deployed in 2.21.13

1 Like