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)]
        )
    ]
)

Test plan

  • Unit tests for RelationshipFilter class
  • Many-to-many relationship tests
  • Many-to-one relationship tests
  • Negative filtering (NOT EXISTS) tests
  • MultiFilter JSON API integration tests
  • Type checking passes (mypy, pyright)
  • Linting passes (ruff)

Closes #364
Closes #505

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.
@cofin cofin force-pushed the feat/relationship-filters branch from fd03568 to ae4a4a1 Compare December 15, 2025 15:57
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.
@codecov-commenter
Copy link

Codecov Report

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

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   80.34%   80.31%   -0.03%     
==========================================
  Files          87       87              
  Lines        6431     6534     +103     
  Branches      838      859      +21     
==========================================
+ Hits         5167     5248      +81     
- Misses       1000     1016      +16     
- Partials      264      270       +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
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