rust diesel method `filter` exists for schema table, but its trait bounds were not satisfied?

Advertisements

Here’s my working env:

  • rustc – 1.57.0
  • cargo – 1.57.0
  • diesel – 1.4.8 with mysql features

Here’s the structure of project:

- src
   - lib.rs 
   + quant 
      - mod.rs
      + common
         - mod.rs
         + persistence
            - mod.rs
            - database.rs
            - model.rs
            - schema.rs
Cargo.toml
diesel.toml

Here’s the lib.rs:

#[macro_use]  
extern crate diesel;

pub mod quant;

Here’s the model.rs:

use diesel::Queryable;

#[derive(Queryable)]
pub struct NetWorthModel {
    pub fund_code: String,
    pub date: String,
    pub create_time: i64,
    pub update_time: i64,
    pub payload: String,
}

Here’s the schema.rs:

use diesel::table;

table! {
    tb_net_worth(fund_code) {
        fund_code -> VarChar,
        date -> Date,
        create_time -> BigInt,
        update_time -> BigInt,
        payload -> Text,
    }
}

Here’s the database.rs:

use crate::quant::common::persistence::model::NetWorthModel;
use diesel::mysql::MysqlConnection;
use diesel::sql_types;
use diesel::Connection;
use chrono;

type YaDate = chrono::prelude::Date<chrono::prelude::Local>;

pub struct Database {
    connection: MysqlConnection,
}

impl Database {
    const ASC: &'static str = "ASC";
    const DESC: &'static str = "DESC";

    pub fn get() -> Database {
        Database::new()
    }

    pub fn shutdown() {}

    fn new() -> Database {
        use crate::quant::config;

        let engine = "mysql";
        let username = "root";
        let password = "123456";
        let host = "localhost";
        let port = 3306;
        let db = "test";
        let url = format!(
            "{}://{}:{}@{}:{}/{}",
            engine, username, password, host, port, db
        );
        let connection = MysqlConnection::establish(&url)
            .expect(&format!("Failed to connect database:{}-{}", engine, db));
        Database { connection }
    }

    pub fn paged_query_net_worth(
        &mut self,
        fund_code: &str,
        order_by: &str,
        start_date: YaDate,
        end_date: YaDate,
        page_index: i32,
        page_size: i32,
    ) -> Vec<NetWorthModel> {
        use super::schema::tb_net_worth::dsl;

        let query = dsl::tb_net_worth
            .filter(dsl::fund_code.eq(fund_code))
            .filter(dsl::date.ge(start_date))
            .filter(dsl::date.lt(end_date));
        let query = if order_by == Database::ASC {
            query.order(dsl::date)
        } else {
            query.order(dsl::date.desc())
        };
        query
            .limit(page_size)
            .offset(page_index * page_size)
            .load::<NetWorthModel>(&self.connection)
            .unwrap()
    }
}

Here’s the compiling error:

error[E0599]: the method `filter` exists for struct `table`, but its trait bounds were not satisfied
    --> src/quant/common/persistence/database.rs:74:14
     |
74   |               .filter(dsl::fund_code.eq(fund_code))
     |                ^^^^^^ method cannot be called on `table` due to unsatisfied trait bounds
     |
    ::: src/quant/common/persistence/schema.rs:5:1
     |
5    | / table! {
6    | |     tb_net_worth(fund_code) {
7    | |         fund_code -> VarChar,
8    | |         date -> Date,
...    |
12   | |     }
13   | | }
     | | -
     | | |
     | |_method `filter` not found for this
     |   doesn't satisfy `table: Iterator`
     |
     = note: the following trait bounds were not satisfied:
             `table: Iterator`
             which is required by `&mut table: Iterator`
note: the following trait must be implemented
    --> /Users/rolin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:55:1
     |
55   | / pub trait Iterator {
56   | |     /// The type of the elements being iterated over.
57   | |     #[stable(feature = "rust1", since = "1.0.0")]
58   | |     type Item;
...    |
3457 | |     }
3458 | | }
     | |_^
     = help: items from traits can only be used if the trait is in scope
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
     |
3    | use std::iter::Iterator;
     |
3    | use crate::diesel::query_dsl::filter_dsl::FilterDsl;
     |
3    | use crate::diesel::QueryDsl;
     |
3    | use log4rs::filter::Filter;
     |

error[E0599]: the method `eq` exists for struct `columns::fund_code`, but its trait bounds were not satisfied
    --> src/quant/common/persistence/database.rs:74:36
     |
74   |               .filter(dsl::fund_code.eq(fund_code))
     |                                      ^^ method cannot be called on `columns::fund_code` due to unsatisfied trait bounds
     |
    ::: src/quant/common/persistence/schema.rs:5:1
     |
5    | / table! {
6    | |     tb_net_worth(fund_code) {
7    | |         fund_code -> VarChar,
8    | |         date -> Date,
...    |
12   | |     }
13   | | }
     | | -
     | | |
     | |_method `eq` not found for this
     |   doesn't satisfy `columns::fund_code: Iterator`
     |
     = note: the following trait bounds were not satisfied:
             `columns::fund_code: Iterator`
             which is required by `&mut columns::fund_code: Iterator`
note: the following trait must be implemented
    --> /Users/rolin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:55:1
     |
55   | / pub trait Iterator {
56   | |     /// The type of the elements being iterated over.
57   | |     #[stable(feature = "rust1", since = "1.0.0")]
58   | |     type Item;
...    |
3457 | |     }
3458 | | }
     | |_^
     = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
     |
3    | use crate::diesel::ExpressionMethods;
     |

error[E0599]: the method `ge` exists for struct `columns::date`, but its trait bounds were not satisfied
    --> src/quant/common/persistence/database.rs:75:31
     |
75   |               .filter(dsl::date.ge(start_date))
     |                                 ^^ method cannot be called on `columns::date` due to unsatisfied trait bounds
     |
    ::: src/quant/common/persistence/schema.rs:5:1
     |
5    | / table! {
6    | |     tb_net_worth(fund_code) {
7    | |         fund_code -> VarChar,
8    | |         date -> Date,
...    |
12   | |     }
13   | | }
     | | -
     | | |
     | |_method `ge` not found for this
     |   doesn't satisfy `columns::date: Iterator`
     |
     = note: the following trait bounds were not satisfied:
             `columns::date: Iterator`
             which is required by `&mut columns::date: Iterator`
note: the following trait must be implemented
    --> /Users/rolin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:55:1
     |
55   | / pub trait Iterator {
56   | |     /// The type of the elements being iterated over.
57   | |     #[stable(feature = "rust1", since = "1.0.0")]
58   | |     type Item;
...    |
3457 | |     }
3458 | | }
     | |_^
     = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
     |
3    | use crate::diesel::ExpressionMethods;
     |

error[E0599]: the method `lt` exists for struct `columns::date`, but its trait bounds were not satisfied
    --> src/quant/common/persistence/database.rs:76:31
     |
76   |               .filter(dsl::date.lt(end_date));
     |                                 ^^ method cannot be called on `columns::date` due to unsatisfied trait bounds
     |
    ::: src/quant/common/persistence/schema.rs:5:1
     |
5    | / table! {
6    | |     tb_net_worth(fund_code) {
7    | |         fund_code -> VarChar,
8    | |         date -> Date,
...    |
12   | |     }
13   | | }
     | | -
     | | |
     | |_method `lt` not found for this
     |   doesn't satisfy `columns::date: Iterator`
     |
     = note: the following trait bounds were not satisfied:
             `columns::date: Iterator`
             which is required by `&mut columns::date: Iterator`
note: the following trait must be implemented
    --> /Users/rolin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:55:1
     |
55   | / pub trait Iterator {
56   | |     /// The type of the elements being iterated over.
57   | |     #[stable(feature = "rust1", since = "1.0.0")]
58   | |     type Item;
...    |
3457 | |     }
3458 | | }
     | |_^
     = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
     |
3    | use crate::diesel::ExpressionMethods;
     |

error[E0599]: no method named `desc` found for struct `columns::date` in the current scope
   --> src/quant/common/persistence/database.rs:80:35
    |
80  |               query.order(dsl::date.desc())
    |                                     ^^^^ method not found in `columns::date`
    |
   ::: /Users/rolin/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.8/src/expression_methods/global_expression_methods.rs:397:8
    |
397 |       fn desc(self) -> Desc<Self> {
    |          ---- the method is available for `columns::date` here
    |
   ::: src/quant/common/persistence/schema.rs:5:1
    |
5   | / table! {
6   | |     tb_net_worth(fund_code) {
7   | |         fund_code -> VarChar,
8   | |         date -> Date,
...   |
12  | |     }
13  | | }
    | |_- method `desc` not found for this
    |
    = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
    |
3   | use crate::diesel::ExpressionMethods;
    |

For more information about this error, try `rustc --explain E0599`.

Would you please help me with it?

>Solution :

The error messages have help annotations that can push you in the right direction:

the following traits are implemented but not in scope

The functions filter, eq, ge, lt, and desc provided by diesel are implemented via traits. And traits must be brought into scope for them to be used. You need a couple of the most common diesel traits: QueryDsl and ExpressionMethods.

So add these to your imports:

use diesel::expression_methods::ExpressionMethods;
use diesel::query_dsl::QueryDsl;

Or if your file is very diesel-heavy, consider using the prelude to wildcard import many of diesels most common types:

use diesel::prelude::*;

The error mentioning Iterator is a red herring. The Iterator trait is always in scope by default, it has functions with matching names, and it has a blanket implementation on &mut T given that T is an Iterator.

Leave a ReplyCancel reply