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
- Create two related entities using a One-To-Many relation
- Create a query using
has_related and a Condition::any().add(cond1).add(cond2)
- 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
Description
When building a query using
.has_related(entity, Condition::any().add(cond1).add(cond2)), the generated SQL statement adds anWHERE cond1 OR cond2 OR A.id = B.a_idclause in theEXISTSsubquery. This leads to incorrect results for the query.Steps to Reproduce
has_relatedand aCondition::any().add(cond1).add(cond2)Expected Behavior
The generated
EXISTSsubquery should have aWHERE (cond1 OR cond2) AND A.id = B.a_idclause, leading to the correct selection of rowsActual Behavior
The generated
EXISTSsubquery shows `WHERE cond1 OR cond2 OR A.id = B.a_id clause, leading to incorrect selection of rowsReproduces How Often
Reproduces every time
Workarounds
Wrapping the
Condition::any()in aCondition::all()seems to generating the correct query. Looking at thesrc/query/join.rssource file, the assumption made is that the provided condition will always be a single column condition, which will be cast to aConditon::all()condition with the.into_collection()call. Unfortunately, providing aCondition::any()condition, thecondition.add()call willnot change the condition type...Versions
sea-orm 2.0.0-rc40
SQLite
Linux Fedora 44