From 5d4c51015ecf69cc019fe68c4fe3bd4453b38f19 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 10 Jun 2024 15:23:26 -0400 Subject: [PATCH] fix: only apply load through for attributes that are being selected directly fix: ensure we lateral join with `from_many?: true` or any `:many` cardinality relationships --- lib/ash/actions/read/read.ex | 3 +-- lib/ash/actions/read/relationships.ex | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ash/actions/read/read.ex b/lib/ash/actions/read/read.ex index 4ad9a0e1..716cb500 100644 --- a/lib/ash/actions/read/read.ex +++ b/lib/ash/actions/read/read.ex @@ -432,7 +432,6 @@ defmodule Ash.Actions.Read do ), query <- Map.put(query, :filter, filter), query <- Ash.Query.unset(query, :calculations), - query <- add_relationship_count_aggregates(query), {%{valid?: true} = query, before_notifications} <- run_before_action(query), {:ok, count} <- fetch_count( @@ -1192,7 +1191,7 @@ defmodule Ash.Actions.Read do query.resource |> Ash.Resource.Info.attributes() |> Enum.filter(fn %{name: name, type: type, constraints: constraints} -> - Ash.Type.can_load?(type, constraints) && Ash.Query.selecting?(query, name) + Ash.Type.can_load?(type, constraints) && (is_nil(query.select) || name in select) end) |> Enum.map(& &1.name) |> Enum.reduce(query.load_through, fn name, load_through -> diff --git a/lib/ash/actions/read/relationships.ex b/lib/ash/actions/read/relationships.ex index 11c6a68a..8b6cb4b6 100644 --- a/lib/ash/actions/read/relationships.ex +++ b/lib/ash/actions/read/relationships.ex @@ -921,7 +921,8 @@ defmodule Ash.Actions.Read.Relationships do false limit == 1 && is_nil(relationship.context) && is_nil(relationship.filter) && - is_nil(relationship.sort) && relationship.type != :many_to_many -> + is_nil(relationship.sort) && relationship.cardinality != :many && + !Map.get(relationship, :from_many?) -> has_parent_expr?(relationship) limit == 1 && (source_data == :unknown || Enum.count_until(source_data, 2) == 1) &&