Skip to content

has_related with Condition::any() end up generating a query with OR A.id = B.Id instead of ANDΒ #3115

Description

@thuttinpasseron

Description

When building a query using .has_related(entity, Condition::any().add(cond1).add(cond2)), the generated SQL statement adds an WHERE cond1 OR cond2 OR A.id = B.a_id clause in the EXISTS subquery. This leads to incorrect results for the query.

Steps to Reproduce

  1. Create two related entities using a One-To-Many relation
  2. Create a query using has_related and a Condition::any().add(cond1).add(cond2)
  3. Execute query

Expected Behavior

The generated EXISTS subquery should have a WHERE (cond1 OR cond2) AND A.id = B.a_id clause, leading to the correct selection of rows

Actual Behavior

The generated EXISTS subquery shows `WHERE cond1 OR cond2 OR A.id = B.a_id clause, leading to incorrect selection of rows

Reproduces How Often

Reproduces every time

Workarounds

Wrapping the Condition::any() in a Condition::all() seems to generating the correct query. Looking at the src/query/join.rs source file, the assumption made is that the provided condition will always be a single column condition, which will be cast to a Conditon::all() condition with the .into_collection() call. Unfortunately, providing a Condition::any() condition, the condition.add() call willnot change the condition type...

Versions

sea-orm 2.0.0-rc40
SQLite
Linux Fedora 44

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions