Skip to content

Conversation

@cofin
Copy link
Member

@cofin cofin commented Dec 15, 2025

Summary

Adds RelationshipFilter class enabling efficient filtering across SQLAlchemy relationships using EXISTS subqueries. This addresses:

Features

  • Support for one-to-many, many-to-one, many-to-many relationships
  • Nested relationship filtering (3+ levels)
  • negate parameter for NOT EXISTS queries
  • CollectionFilter auto-detection of relationship attributes
  • MultiFilter JSON API integration

Example Usage

# Filter orders by customer country (one-to-many)
RelationshipFilter(
    relationship="customer",
    filters=[CollectionFilter("country", ["USA", "Canada"])]
)

# Filter users by tags (many-to-many)
RelationshipFilter(
    relationship="tags",
    filters=[CollectionFilter("name", ["admin", "moderator"])]
)

# Nested filtering (Order -> Customer -> Tier)
RelationshipFilter(
    relationship="customer",
    filters=[
        RelationshipFilter(
            relationship="tier",
            filters=[ComparisonFilter("level", "ge", 3)]
        )
    ]
)

Closes #364
Closes #505

@codecov-commenter
Copy link

codecov-commenter commented Dec 15, 2025

Codecov Report

❌ Patch coverage is 79.52756% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.09%. Comparing base (8916358) to head (cc4261a).

Files with missing lines Patch % Lines
advanced_alchemy/filters.py 79.52% 16 Missing and 10 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #639      +/-   ##
==========================================
- Coverage   79.09%   79.09%   -0.01%     
==========================================
  Files          99       99              
  Lines        7861     7964     +103     
  Branches     1065     1086      +21     
==========================================
+ Hits         6218     6299      +81     
- Misses       1301     1317      +16     
- Partials      342      348       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cofin cofin marked this pull request as draft December 20, 2025 17:40
@cofin cofin force-pushed the feat/relationship-filters branch 3 times, most recently from 3928822 to cc4261a Compare January 19, 2026 20:45
cofin added 9 commits January 19, 2026 16:11
Adds RelationshipFilter class enabling efficient filtering across SQLAlchemy
relationships using EXISTS subqueries. This addresses two issues:

- Issue litestar-org#364: Add StatementFilters for relationships & association patterns
- Issue litestar-org#505: Fix MultiFilter with Many-to-Many generating incorrect SQL

Features:
- Support for one-to-many, many-to-one, many-to-many relationships
- Nested relationship filtering (3+ levels)
- negate parameter for NOT EXISTS queries
- CollectionFilter auto-detection of relationship attributes
- MultiFilter JSON API integration

The implementation uses correlated EXISTS subqueries for optimal query
performance with single database round-trip regardless of nesting depth.
Adds integration tests covering:
- Many-to-many relationship filtering
- Many-to-one relationship filtering
- Negative filtering (NOT EXISTS)
- CollectionFilter delegation to RelationshipFilter
- MultiFilter JSON API integration

Tests use existing uuid_test_session fixtures for consistency.
Mock engines don't support actual database operations, so the
RelationshipFilter tests cannot run meaningfully with them. This
follows the same pattern used in test_filters.py and other
integration tests.
Spanner doesn't support direct UNIQUE constraints, which are used
in the UUID test models (IntegrationUUIDTag.name has unique=True).
Move Spanner and Oracle skips to dedicated fixtures that run before
the uuid_test_session fixtures. This ensures the skip happens before
schema creation, which fails on Spanner (UNIQUE constraints) and
Oracle (schema isolation with xdist groups).
Add pytest_collection_modifyitems hook to skip relationship filter
tests for Spanner and Oracle engines before fixtures are set up.
This prevents errors from schema creation with UNIQUE constraints
that Spanner doesn't support.
MySQL (asyncmy) has the same schema isolation issues with xdist
groups as Oracle, causing "table doesn't exist" errors.
@cofin cofin force-pushed the feat/relationship-filters branch from cc4261a to 274a49c Compare January 19, 2026 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MultiFilter with Many-to-Many relationships Enhancement: Add StatementFilters for relationships & association object patterns

2 participants