fix: deduplicate relationship joins when searchable_fields and facet_fields reference the same model (#217)

This commit is contained in:
d3vyce
2026-04-02 11:09:26 +02:00
committed by GitHub
parent bce71bfd42
commit 0b17c77dee
3 changed files with 76 additions and 5 deletions

View File

@@ -116,8 +116,12 @@ def _apply_joins(q: Any, joins: JoinType | None, outer_join: bool) -> Any:
def _apply_search_joins(q: Any, search_joins: list[Any]) -> Any:
"""Apply relationship-based outer joins (from search/filter_by) to a query."""
seen: set[str] = set()
for join_rel in search_joins:
q = q.outerjoin(join_rel)
key = str(join_rel)
if key not in seen:
seen.add(key)
q = q.outerjoin(join_rel)
return q

View File

@@ -242,13 +242,19 @@ async def build_facets(
else:
q = select(column).select_from(model).distinct()
# Apply base joins (already done on main query, but needed here independently)
# Apply base joins (deduplicated) — needed here independently
seen_joins: set[str] = set()
for rel in base_joins or []:
q = q.outerjoin(rel)
rel_key = str(rel)
if rel_key not in seen_joins:
seen_joins.add(rel_key)
q = q.outerjoin(rel)
# Add any extra joins required by this facet field that aren't already in base_joins
# Add any extra joins required by this facet field that aren't already applied
for rel in rels:
if str(rel) not in existing_join_keys:
rel_key = str(rel)
if rel_key not in existing_join_keys and rel_key not in seen_joins:
seen_joins.add(rel_key)
q = q.outerjoin(rel)
if base_filters: