柴油机的滤清器使用方法

How to use diesel's filter methods

我有一个小的 actix web 项目。有这样一个模型:

#[derive(Serialize, Deserialize, Insertable, Identifiable, Queryable, PartialEq, Debug)]
#[table_name = "confirmations"]
pub struct Confirmation {
    pub id: Uuid,
    pub email: String,
    pub expires_at: chrono::NaiveDateTime
}

然后我有一个函数,我只想从数据库中获取项目。 我尝试这样做:

pub fn get_confirmation_item(
    id: &Uuid,
    email: &str,
    pool: &web::Data<PgDBPool>
) -> Result<Confirmation, AppError> {
    let connection = pool.get().unwrap();
    let result = confirmations
        .filter(id.eq(id))
        .filter(email.eq(email))
        .select(id)
        .first(&connection);
    Ok(result)
}

最后我得到了这个错误:

error[E0277]: the trait bound `bool: diesel::Expression` is not satisfied
  --> src/apps/users/utils.rs:45:17
   |
45 |         .filter(id.eq(id))
   |                 ^^^^^^^^^ the trait `diesel::Expression` is not implemented for `bool`
   |
   = note: required because of the requirements on the impl of `diesel::query_dsl::filter_dsl::FilterDsl<bool>` for

diesel::query_builder::SelectStatement<schema::confirmations::table>

error[E0277]: the trait bound `bool: diesel::expression::NonAggregate` is not satisfied
  --> src/apps/users/utils.rs:45:17
   |
45 |         .filter(id.eq(id))
   |                 ^^^^^^^^^ the trait `diesel::expression::NonAggregate` is not implemented for `bool`
   |
   = note: required because of the requirements on the impl of `diesel::query_dsl::filter_dsl::FilterDsl<bool>` for

diesel::query_builder::SelectStatement<schema::confirmations::table>

error[E0277]: the trait bound `diesel::query_builder::SelectStatement<schema::confirmations::table,

diesel::query_builder::select_clause::DefaultSelectClause, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::WhereClause>: diesel::query_dsl::filter_dsl::FilterDsl<>is not satisfied --> src/apps/users/utils.rs:46:10 | 46 | .filter(email.eq(email)) | ^^^^^^ the traitdiesel::query_dsl::filter_dsl::FilterDsl<>is not implemented fordiesel::query_builder::SelectStatement<schema::confirmations::table, diesel::query_builder::select_clause::DefaultSelectClause, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::WhereClause>` | = help: the following implementations were found: <diesel::query_builder::SelectStatement<F, S, D, W, O, L, Of, G, LC> as diesel::query_dsl::filter_dsl::FilterDsl>

error: aborting due to 3 previous errors

有人知道如何解决这个错误吗? Cargo.toml 中的依赖项:

[dependencies]
actix-web = "3.1.0"
actix-rt = "1.1.1"
diesel = { version = "1.4.5", features = ["postgres", "uuidv07", "r2d2", "chrono"] }
r2d2 = "0.8.9"
derive_more = "0.99.11"
bson = "1.1.0"
lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.59"
jsonwebtoken = "7.2.0"
envfile = "0.2.1"
env_logger = "0.8.1"
chrono = { version = "0.4.19", features = ["serde"] }
rust-crypto = "0.2.36"
uuid = { version = "0.8.1", features = ["serde", "v4"] }
futures = "0.3.7"
lettre = { git = "https://github.com/lettre/lettre" }

您将 idemail 与他们自己进行比较。您想要的是将数据库字段的值与代码中的值进行比较。

对于柴油机,这通常意味着您需要导入架构,如下所示:

use schema::confirmation;

// ..
.filter(confirmation::id.eq(id))
// ..

另请查看一些示例的文档:https://diesel.rs/guides/getting-started/


与这个问题无关,你的id不是独一无二的吗?如果是这样,您还可以使用 find 方法,该方法允许您通过主键进行搜索。

哦,还有,您不需要使用 filter 两次。有一种方法 .and 允许您编写如下查询:schema::id.eq(id).and(schema::email.eq(email))