简体   繁体   English

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

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

Here's my working env:这是我的工作环境:

  • rustc - 1.57.0生锈 - 1.57.0
  • cargo - 1.57.0货物 - 1.57.0
  • diesel - 1.4.8 with mysql features柴油机 - 1.4.8 具有 mysql 功能

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 :这是lib.rs

#[macro_use]  
extern crate diesel;

pub mod quant;

Here's the model.rs :这是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 :这是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 :这是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?你能帮我吗?

The error messages have help annotations that can push you in the right direction:错误消息具有帮助注释,可以将您推向正确的方向:

the following traits are implemented but not in scope以下特征已实现,但未在 scope 中实现

The functions filter , eq , ge , lt , and desc provided by diesel are implemented via traits.柴油提供的功能filtereqgeltdesc是通过特征实现的。 And traits must be brought into scope for them to be used.并且必须将特征带入 scope 才能使用。 You need a couple of the most common diesel traits: QueryDsl and ExpressionMethods .您需要几个最常见的柴油特性: QueryDslExpressionMethods

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.提到Iterator的错误是一个红鲱鱼。 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 . Iterator trait 默认始终位于 scope 中,它具有名称匹配的函数,并且鉴于T是一个Iterator ,它在&mut T上有一个全面的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM