Skip to content

Operators have undefined effects on toMany relationships #1110

@Helveg

Description

@Helveg

While working more on #1064 and related issues, I noticed that the current nestjs-paginate semantics don’t correctly handle to-many relationship filters.

I think it’s best illustrated by these cases:

The root cause is that filters on related columns use WHERE against joined tables, which filters the related rows rather than the parent entity. To behave correctly, these should instead be built using EXISTS / NOT EXISTS conditions that determine inclusion at the parent level.

Proposed plan:

  • Keep $not and $null semantics unchanged for direct columns.

  • Switch relationship filters from WHERE to EXISTS logic.

  • Define clear semantics for $not, $none, and $null on relationships:

Operator Description Example
$op Include entities where one or more related entities match the operation Find cats with red toys
$not:$op Include entities where one or more related entities match the negated operation (excluding those without related entities) Find cats with toys that are not red
$none:($not:)$op Include entities where zero related entities match the operation (including those without related entities) Find cats without (non-)red toys
$null Include entities with zero related entities Find cats without toys
$not:$null Include entities with one or more related entities Find cats with toys

All these should filter only the parent entity, leaving the returned joined relationships untouched. I also propose that we apply `$null` to the relationship rather than the primary column, since that doesn't work well with compound primary columns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions