Skip to content

Commit 202d55b

Browse files
committedMar 30, 2025·
chore: only use subquery for through resource if it has joins
1 parent 305b7cc commit 202d55b

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed
 

‎lib/data_layer.ex

+15-11
Original file line numberDiff line numberDiff line change
@@ -1182,18 +1182,26 @@ defmodule AshPostgres.DataLayer do
11821182
{:ok, through_query} ->
11831183
through_query = Ecto.Query.exclude(through_query, :select)
11841184

1185+
through_query =
1186+
if through_query.joins && through_query.joins != [] do
1187+
subquery(
1188+
set_subquery_prefix(
1189+
through_query,
1190+
source_query,
1191+
relationship.through
1192+
)
1193+
)
1194+
else
1195+
through_query
1196+
end
1197+
11851198
if query.__ash_bindings__[:__order__?] do
11861199
subquery =
11871200
subquery(
11881201
from(
11891202
destination in query,
11901203
select_merge: %{__order__: over(row_number(), :order)},
1191-
join:
1192-
through in subquery(set_subquery_prefix(
1193-
through_query,
1194-
source_query,
1195-
relationship.through
1196-
)),
1204+
join: through in ^through_query,
11971205
as: ^through_binding,
11981206
on:
11991207
field(through, ^destination_attribute_on_join_resource) ==
@@ -1227,11 +1235,7 @@ defmodule AshPostgres.DataLayer do
12271235
from(
12281236
destination in query,
12291237
join:
1230-
through in subquery(set_subquery_prefix(
1231-
through_query,
1232-
source_query,
1233-
relationship.through
1234-
)),
1238+
through in ^through_query,
12351239
as: ^through_binding,
12361240
on:
12371241
field(through, ^destination_attribute_on_join_resource) ==

‎test/support/resources/post.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ defmodule AshPostgres.Test.Post do
533533
public?(true)
534534

535535
destination_attribute(:post_id)
536-
filter expr(not is_nil(post.id))
536+
filter(expr(not is_nil(post.id)))
537537
end
538538

539539
many_to_many :co_authors, AshPostgres.Test.Author do

0 commit comments

Comments
 (0)