ash icon indicating copy to clipboard operation
ash copied to clipboard

Assumption failed: got a query without a domain

Open sevenseacat opened this issue 1 year ago • 3 comments

Describe the bug

I'm probably doing something a bit weird, but I wanted to see if calculations on embedded resources somehow had access to their parent resource.

(I find myself wanting something like this fairly often, or eg. loading one calculation on a top level resource and passing data from the result down through relationships to be stored on the children)

So I whipped up this:

defmodule MyApp.Dispatch.EmailRecipient do
  use Ash.Resource, data_layer: :embedded

  # lots of other code 

  calculations do
    calculate :foo, :string, fn one, two ->
      dbg()
      "foo"
    end
  end
end

This resource is embedded in a Payload resource (which is a union, but I don't think that matters), which is then embedded in a Notification resource.

So I loaded a Notification and went into it to get the EmailRecipient:

iex(1)> n = Ash.get!(MyApp.Dispatch.Notification, "0043864d-15be-4ce2-876c-57d0171d2fd8")
#MyApp.Dispatch.Notification<...>
iex(3)> r = hd(n.payload.value.recipients)
#MyApp.Dispatch.EmailRecipient<
  foo: #Ash.NotLoaded<:calculation, field: :foo>,
  ...
>

And then tried to load the foo calculation:

iex(4)> Ash.load(r, :foo)
** (Ash.Error.Framework.AssumptionFailed) Assumption failed: got a query without a domain

This should not be possible, please report a detailed bug at:

https://github.com/ash-project/ash/issues/new?assignees=&labels=bug%2C+needs+review&template=bug_report.md&title=

    (ash 3.4.38) lib/ash/actions/read/read.ex:41: Ash.Actions.Read.run/3
    (ash 3.4.38) lib/ash.ex:1858: Ash.load/3
    (ash 3.4.38) lib/ash.ex:1812: Ash.load/3
    iex:4: (file)

Using reuse_values?: true also raises the same error.

It tells me to report a bug, so here I am!

Expected behavior

I'd get the debug printout of whatever the calculation sees, to answer my question if somehow it knows about where its embedded in (I suspect the answer is no, but still). And the calculation would successfully calculate the value "foo".

Runtime

  • Elixir version 1.16.3-otp-26
  • Erlang version 26.2.5.1
  • OS macOS Sonoma
  • Ash version 3.4.38

sevenseacat avatar Dec 10 '24 07:12 sevenseacat

Hmmm... you could try configurIng a domain on the embedded resource, or specifying the domain option to Ash.load. If we can't determine a domain then we can't do any action logic, so we probably just need a better error message there?

zachdaniel avatar Dec 10 '24 14:12 zachdaniel

I've not actually used embedded resources before, so if they're not supposed to be used like this, that's fine! This issue is here for posterity if other people try to do it and wonder why it doesn't work :)

I'll give those options a go in the morning - but I don't think a domain should be specified on an embedded resource, should it? Or can you embed a resource cross-domain?

sevenseacat avatar Dec 10 '24 15:12 sevenseacat

We may actually prevent specifying a domain on an embedded resource now that I think of it. The idea is that since they are meant to be used as a type, their domain would always be inherited from whichever resource contains them.

I forget, you may have to try it out to confirm. But I do think specifying the domain option ought to be supported.

zachdaniel avatar Dec 10 '24 15:12 zachdaniel