How to break sea-orm query using multiline statement

148 Views Asked by At

I am a beginner here using sea-orm. I am trying to break down query for filtering my data. params are input from http query parameters. Based on value of params i want to filter data.

let test = ProductEntity::find();

if params.get("price_gt").is_some() {
    test.filter(product_entity::Column::Price.eq(20));
}

dbg!(test.all(&db).await?);

Help meIssue

1

There are 1 best solutions below

0
On

It is a little late, but it can be useful for others. Entity uses the Build pattern. As you can see here filter takes mut self as argument but also returns self.

In this specific case, we have to make test mutable: let mut test = ..., and in the if statement, we have to return ownership to the test variable:

test = test.filter(product_entity::Column::Price.eq(20));

Then it should work as expected.