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

Loading relationships on a loaded resource seems to unload other nested calculations #1825

Open
sevenseacat opened this issue Feb 26, 2025 · 3 comments
Labels
bug Something isn't working needs review

Comments

@sevenseacat
Copy link
Contributor

sevenseacat commented Feb 26, 2025

Describe the bug

We're seeing a weird issue with calling Ash.load to load relationships on already-loaded data. It seems to unload other calculations on related data, which definitely shouldn't happen?

To Reproduce

I've tried to pare down these resources as much as possible - ping me if they don't make sense anymore.

defmodule MyApp.OrgOpportunity do 
  relationships do
    belongs_to :org, MyApp.Org, allow_nil?: false
    belongs_to :opportunity, MyApp.Opportunity, allow_nil?: false
  end
end

defmodule MyApp.Opportunity do 
  relationships do
    belongs_to :google_place, Google.Place, allow_nil?: false
  end

  calculations do
    # I tried making this an aggregate instead, no difference
    calculate :google_review_score, :float, expr(google_place.review_score)
  end
end
iex> oo = Ash.get!(MyApp.OrgOpportunity, uuid, load: [opportunity: :google_review_score], actor: user)
#MyApp.OrgOpportunity<
  org: #Ash.NotLoaded<:relationship, field: :org>,
  opportunity: #MyApp.Opportunity<
    google_review_score: 5.0,
    ...
  >,
  ...
>

iex> Ash.load!(oo, :org, actor: user)
#MyApp.OrgOpportunity<
  org: #MyApp.Org<...>,
  opportunity: #MyApp.Opportunity<
    google_review_score: #Ash.NotLoaded<:calculation, field: :google_review_score>,
    ...
  >,
  ...
>

Because we want to do this to reload a subset of related data (instead of reloading the whole resource and all of its related data), the only way I can think of to get this to work is to specifically unload the relationship and then load it again with lazy?: true so nothing else is affected, that seems to work.

eg.

iex> # If org was already loaded on the OrgOpportunity record
iex> oo2 = Ash.Resource.unload(oo, :org, actor: user)
iex> Ash.load!(oo2, :org, actor: user, lazy?: true)
iex> # This would preserve other data like google_review_score on the related opportunity

But I don't think I should need to do this?

Expected behavior

I wouldn't expect the google_review_score to be unloaded when loading something else.

Runtime

  • Elixir version 1.18.2-otp-27
  • Erlang version 27.2
  • OS macOS Sonoma
  • Ash version 3.4.65
@sevenseacat sevenseacat added bug Something isn't working needs review labels Feb 26, 2025
@zachdaniel
Copy link
Contributor

Hey @sevenseacat would it be possible to provide a full reproduction or failing test case?

@zachdaniel
Copy link
Contributor

This is definitely strange.

@sevenseacat
Copy link
Contributor Author

I'll put one together tomorrow 👍

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

2 participants