schema table 存在 rust diesel 方法 `filter`,但不满足其特征界限?

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

这是我的工作环境:

项目结构如下:

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

这是lib.rs

#[macro_use]  
extern crate diesel;

pub mod quant;

这是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,
}

这是schema.rs

use diesel::table;

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

这是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()
    }
}

编译错误如下:

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`.

你能帮我解决一下吗?

错误消息包含帮助注释,可以将您推向正确的方向:

the following traits are implemented but not in scope

diesel提供的函数filtereqgeltdesc都是通过traits实现的。并且必须将特征纳入范围以供使用。您需要几个最常见的柴油特性:QueryDsl and ExpressionMethods.

所以将这些添加到您的导入中:

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

或者,如果您的文件非常重柴油,请考虑使用通配符导入许多柴油最常见类型的前奏:

use diesel::prelude::*;

提到 Iterator 的错误是转移注意力的错误。默认情况下,Iterator 特征总是在范围内,它具有名称匹配的函数,并且它在 &mut T 上有一个全面的实现,因为 T 是一个 Iterator.