Replies: 1 comment
-
Love this idea! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I’d like to propose a feature that would significantly improve the user experience when filtering records by related entities. In frameworks like Laravel, we often rely on
has
andwhereHas
methods to filter entities based on the existence or attributes of their relations, without depending on complex JOIN logic. A similar approach in SeaORM could be incredibly beneficial.Core Proposal:
has
andhas_filter
1)
has(Relation)
WHERE EXISTS(...)
orWHERE parent.id IN (...)
subqueries.2)
has_filter(Relation, subquery_builder)
Why Use Subqueries Instead of
INNER JOIN
?If multiple parts of the code add the same JOIN, you risk collisions or naming conflicts. Subqueries isolate that logic.
Subqueries keep your primary query simpler, instead of expanding it with multiple JOIN clauses.
Many databases handle
WHERE EXISTS(...)
,WHERE IN(...)
, andJOIN
similarly, so performance differences are often minimal.(Ref: 【ガチ検証】Eloquent whereHas() はもう遅くないよ!)
Why This Is Useful
Instead of manually constructing subqueries or joins,
has
andhas_filter
would let you directly reuse existing relationship definitions.Users coming from ORMs like Laravel’s Eloquent (or similar frameworks) will feel right at home.
We can extend the same pattern to count-based filtering or more complex subquery conditions without complicating userland code.
Optional: Count-Based Variants
In some frameworks, you can specify conditions like “users who have more than N sessions.” In Rust, we might prefer separate methods rather than a single function with optional parameters:
has_more_than(Relation, n)
→ filters entities whose related record count is> n
has_less_than(Relation, n)
→< n
has_no_less_than(Relation, n)
→>= n
has_no_more_than(Relation, n)
→<= n
has_exact(Relation, n)
→= n
And possibly for
has_filter
variants:has_filter_more_than
,has_filter_less_than
,has_filter_no_less_than
,has_filter_no_more_than
,has_filter_exact
.Example Usage
These could be implemented using subqueries with
COUNT(*)
checks, for example:Whether to rely on
EXISTS
,IN
, or explicitCOUNT
is a design/implementation detail, but having a straightforward SeaORM API would reduce repetitive query code.Final Thoughts
has
andhas_filter
for subquery-based filtering, leveraging existing relationship definitions.has_more_than
,has_exact
, etc. for advanced use cases.EXISTS
,IN
,COUNT
, etc.) while offering a user-friendly, relationship-driven syntax.Thank you for your time and consideration!
Beta Was this translation helpful? Give feedback.
All reactions