Fix nested filter on self-referencing relationships #3096
+19
−4
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.
Why make this change?
Closes #3028
GraphQL nested filters on self-referencing relationships (e.g.,
category.parent → category) return incorrect results because the foreign key lookup uses entity names only, which are identical for self-joins.What is this change?
In
HandleNestedFilterForSql, replacedAddJoinPredicatesForRelatedEntitywithAddJoinPredicatesForRelationship:EntityRelationshipKeyusing both entity name and relationship name (filterField.Name)AddJoinPredicatesForRelationshipalready handles both self-joins and regular relationships internallyBefore (broken for self-joins):
After:
How was this tested?
Sample Request(s)
Original prompt
This section details on the original issue you should resolve
<issue_title>[Bug]: Nested filter on Self-Referencing Relationships returns incorrect results</issue_title>
<issue_description>### What happened?
Problem
When using GraphQL nested filters on self-referencing relationships (e.g., parent/child hierarchy), the filter returns incorrect results.
This query gives incorrect results
Expected Behavior
Returns book items with categories whose parent's name contains "Classic".
Proposed Solution
HandleNestedFilterForSql, useAddJoinPredicatesForRelationshipinstead ofAddJoinPredicatesForRelatedEntityEntityRelationshipKeyusing the relationship name (filter field name) to look up the correct FK definitionfkLookupKey:{queryStructure.EntityName, filterField.Name}targetEntityName: the nested filter entity namesubqueryTargetTableAlias: the EXISTS subquery's source aliasIn
BaseGraphQLFilterParsers.cs: