1

I want to add multiple conditions where clause using diesel filter for my joined table, below is the code that I tried that is not working for me. I want to check some conditions before adding multiple filter queries.

fn find_accounts_details(
    &self, 
    employee_id: &str,
    start_date: Option<DateTime<Utc>>,
    end_date: Option<DateTime<Utc>>)anyhow::Result<Vec<model::Accounts>>{

    let filter_ = employee::dsl::employee_id.eq(employee_id);
  
    if start_date.is_some() && end_date.is_some() {
      filter_.and(account::created_at.between(start_date, end_date));
    };

   let employee_account = employee::table
.inner_join(account::table.on(employee::dsl::employee_id.eq(account::dsl::employee_id)),)
 .filter(filter_) --> Not working here 
 .load::<(Employee_Details,)>(&self.pool.get()?)?;
}
#Emplyee table

| employee-id  | employee_name | empolyee_email|       
| -----------  | --------------|-------------  |
| 1            | ABC           |abc@mail.com   |
| 2            | xyz           |xyz@mail.com   |


# Account table

| account  | employee-id    | account-balnce | created_at|
| -------- | ----------     |--------------- |-----------|
| 1        | 1              |   2000         | 22/10/2021|
| 2        | 2              |   5000         | 01/09/2021|

nagaraj
  • 797
  • 1
  • 6
  • 29
  • "Not working" -> do you get an error, if yes which? What is the output you expect, what do you get instead? – orlp Nov 30 '21 at 12:22
  • I don't get an error, only it takes the first filters command `let filter_ = employee::dsl::employee_id.eq(employee_id);` gives all the results. it doesn't append or add the second command. – nagaraj Nov 30 '21 at 12:31
  • Does this answer your question? [Rust diesel conditionally filter a query](https://stackoverflow.com/questions/65039754/rust-diesel-conditionally-filter-a-query) – weiznich Nov 30 '21 at 12:53
  • I have seen it, but it works for single table. I am not sure for multiple table. – nagaraj Nov 30 '21 at 13:48
  • If it's not a duplicate of the first thread I've linked it's a duplicate of https://stackoverflow.com/questions/62161446/diesel-boxableexpressions-generic-over-a-table-and-its-joins/62169975#62169975 – weiznich Nov 30 '21 at 16:41

0 Answers0