ash-project / ash-project/ash

Assumption failed: got a query without a domain

Open
#1,649 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Elixir
Stars
2.5k
Forks
422
Avg merge
23h 26m
Merged PRs (30d)
46

Description

**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:

```elixir
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

Contributor guide

Open the contributing guide

Research direction

Reproduce the embedded EmailRecipient calculation through Ash.load/3, then start at lib/ash/actions/read/read.ex:41 and follow the Ash.load/3 path in lib/ash.ex:1812 and 1858. Done means loading :foo no longer raises the query-without-a-domain assumption failure and the calculation returns "foo"; the issue also asks whether parent-resource context is available.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.