Add WhereHas and WhereDoesntHave helpers for relationship filtering#7642
Open
vzaica wants to merge 10 commits intogo-gorm:masterfrom
Open
Add WhereHas and WhereDoesntHave helpers for relationship filtering#7642vzaica wants to merge 10 commits intogo-gorm:masterfrom
vzaica wants to merge 10 commits intogo-gorm:masterfrom
Conversation
Contributor
|
These helpers hook directly into GORM’s chainable API by recording per-statement relationship predicates that are materialized as EXISTS/NOT EXISTS subqueries for every association type, including polymorphic chains, while cloning/merging safeguards and metadata checks ensure the scopes remain stable across nested queries and dry-run flows validated by the new regression suite. Affected Areas• This summary was automatically generated by @propel-code-bot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR covers #3871 feature request.
What did this pull request do?
This PR adds two new query helper methods —
WhereHasandWhereDoesntHave.These methods make it possible to filter query results based on related models, similar to how this is done in frameworks like Laravel Eloquent.
Problem
Currently, GORM lacks convenient methods for filtering records by the existence (or absence) of related entities.
For example, there is no built-in way to express queries like:
or the inverse case using WhereDoesntHave.
As a result, developers must write verbose raw joins or subqueries, which reduces readability and reusability.
Solution
This PR introduces:
Filters parent records that have related models matching the scope.
Filters parent records that do not have related models matching the scope.
These methods apply only to query building (e.g., SELECT) and do not affect update/insert logic.
Implementation notes
User Case Description
Assume the following model structure:
1. Basic WhereHas
Find all users who have pets:
2. Basic WhereDoesntHave
Find all users who don’t have pets:
3. Conditional WhereHas
Find all users who have toys of a specific type:
4. Nested WhereHas
Find all users who have pets without toys: