sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

Adding `filter`s after adding a `Condition::any` in another call results in `OR` instead of `AND`

Open junbl opened this issue 3 years ago • 2 comments

Description

When querying data, adding a filter using a Condition::any causes any further calls to filter to also be part of that any condition.

Steps to Reproduce

  1. Create a query with a filter using a Condition::any
  2. Add another condition in a separate call to filter

Expected Behavior

I'd expect this:

Entity::find()
        .filter(
            Condition::any()
                .add(Column::Field2.eq("dog"))
                .add(Column::Field3.eq("dog")),
        )
        .filter(Column::Field1.eq(1))

to be equivalent to this:

Entity::find()
        .filter(Column::Field1.eq(1))
        .filter(
            Condition::any()
                .add(Column::Field2.eq("dog"))
                .add(Column::Field3.eq("dog")),
        )

Actual Behavior

The first produces:

SELECT `table`.`field1`, `table`.`field2`, `table`.`field3` FROM `table` WHERE `table`.`field2` = 'dog' OR `table`.`field3` = 'dog' OR `table`.`field1` = 1

While the second produces the expected output of:

SELECT `table`.`field1`, `table`.`field2`, `table`.`field3` FROM `table` WHERE `table`.`field1` = 1 AND (`table`.`field2` = 'dog' OR `table`.`field3` = 'dog')

Reproduces How Often

Every time

Versions

sea-orm v0.9.1
    ├── sea-orm-macros v0.9.1 (proc-macro)
    ├── sea-query v0.26.2
    │   ├── sea-query-derive v0.2.0 (proc-macro)
    │   ├── sea-query-driver v0.2.0 (proc-macro)
    ├── sea-strum v0.23.0
    │   └── sea-strum_macros v0.23.0 (proc-macro)

Ubuntu 22.04 on WSL in Windows 11

Additional Information

https://github.com/e-rhodes/sea-orm-filter-and-or

junbl avatar Aug 02 '22 21:08 junbl