Skip to content

Commit 28f2861

Browse files
committed
improvement: optimize relationships with identity on other end
1 parent 6cc36c9 commit 28f2861

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/aggregate.ex

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,9 +1174,22 @@ defmodule AshPostgres.Aggregate do
11741174

11751175
defp single_path?(resource, [relationship | rest]) do
11761176
relationship = Ash.Resource.Info.relationship(resource, relationship)
1177-
relationship.type == :belongs_to && single_path?(relationship.destination, rest)
1177+
1178+
(relationship.type == :belongs_to ||
1179+
has_one_with_identity?(relationship)) &&
1180+
single_path?(relationship.destination, rest)
1181+
end
1182+
1183+
defp has_one_with_identity?(%{type: :has_one} = relationship) do
1184+
relationship.destination
1185+
|> Ash.Resource.Info.identities()
1186+
|> Enum.any?(fn %{keys: keys} ->
1187+
keys == [relationship.destination_field]
1188+
end)
11781189
end
11791190

1191+
defp has_one_with_identity?(_), do: false
1192+
11801193
@doc false
11811194
def aggregate_field(aggregate, resource, _relationship_path, query) do
11821195
case Ash.Resource.Info.field(
@@ -1192,10 +1205,6 @@ defmodule AshPostgres.Aggregate do
11921205

11931206
AshPostgres.Expr.validate_type!(query, calc_type, "#{inspect(calculation.name)}")
11941207

1195-
if aggregate.context == %{} do
1196-
raise "what"
1197-
end
1198-
11991208
{:ok, query_calc} =
12001209
Ash.Query.Calculation.new(
12011210
calculation.name,

0 commit comments

Comments
 (0)