Skip to content

Conversation

@sevenseacat
Copy link
Contributor

When a many_to_many relationship uses a join_relationship with sort/limit/filter, those constraints weren't being applied in the lateral join query.

Two issues:

  1. The sort/limit/filter from the join_relationship weren't being added to the through_query at all
  2. When the through_query is wrapped in a subquery, the parent correlation needs to be inside the subquery so the limit applies per-parent

This is related to ash-project/ash#2486 (hence why some of the code is the same) but for Postgres, which uses lateral joins.

(Claude was used in investigating and fixing this bug)

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

When a many_to_many relationship uses a join_relationship with sort/limit/filter,
those constraints weren't being applied in the lateral join query.

Two issues:

1. The sort/limit/filter from the join_relationship weren't being added to the through_query at all
2. When the through_query is wrapped in a subquery, the parent correlation needs to be inside
the subquery so the limit applies per-parent
@zachdaniel
Copy link
Contributor

I've fixed the other failing tests (there are two flaky ones that might fail still) which may help with the testing story, but so far this LGTM

@zachdaniel zachdaniel merged commit 1f29391 into main Dec 24, 2025
64 of 67 checks passed
@zachdaniel
Copy link
Contributor

`thanks

@sevenseacat sevenseacat deleted the bug/many-to-many-inherits-limit-and-sort branch December 24, 2025 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants