铁锈柴油方法“过滤器”存在模式表,但其特征边界不满足?

lin*_*bin 1 mysql orm rust rust-diesel

这是我的工作环境:

  • rustc-1.57.0
  • 货物 - 1.57.0
  • diesel - 1.4.8 具有 mysql 功能

这是项目的结构:

- src
   - lib.rs 
   + quant 
      - mod.rs
      + common
         - mod.rs
         + persistence
            - mod.rs
            - database.rs
            - model.rs
            - schema.rs
Cargo.toml
diesel.toml
Run Code Online (Sandbox Code Playgroud)

这是lib.rs

#[macro_use]  
extern crate diesel;

pub mod quant;
Run Code Online (Sandbox Code Playgroud)

这是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,
}
Run Code Online (Sandbox Code Playgroud)

这是schema.rs

use diesel::table;

table! {
    tb_net_worth(fund_code) {
        fund_code -> VarChar,
        date -> Date,
        create_time -> BigInt,
        update_time -> BigInt,
        payload -> Text,
    }
}
Run Code Online (Sandbox Code Playgroud)

这是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()
    }
}
Run Code Online (Sandbox Code Playgroud)

这是编译错误:

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`.
Run Code Online (Sandbox Code Playgroud)

你能帮我一下吗?

kmd*_*eko 11

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

以下特征已实现但不在范围内

diesel 提供的filtereqgelt、 等功能是通过 Trait 来实现的。desc并且必须将特征纳入其使用范围。您需要几个最常见的柴油特性:QueryDslExpressionMethods

因此,将这些添加到您的导入中:

use diesel::expression_methods::ExpressionMethods;
use diesel::query_dsl::QueryDsl;
Run Code Online (Sandbox Code Playgroud)

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

use diesel::prelude::*;
Run Code Online (Sandbox Code Playgroud)

提到的错误Iterator是一个转移注意力的事情。默认情况下,该Iterator特征始终在范围内,它具有具有匹配名称的函数,并且它具有一个笼统的实现,&mut T即.TIterator