首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用柴油的过滤方法?

如何使用柴油的过滤方法?
EN

Stack Overflow用户
提问于 2020-11-11 12:02:15
回答 1查看 1.1K关注 0票数 0

我有一个小小的actix网络项目。有这样一种模式:

代码语言:javascript
复制
#[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
}

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

代码语言:javascript
复制
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)
}

最后,我得到了一个错误:

代码语言:javascript
复制
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<bool>>: diesel::query_dsl::filter_dsl::FilterDsl<_>` is not satisfied
  --> src/apps/users/utils.rs:46:10
   |
46 |         .filter(email.eq(email))
   |          ^^^^^^ the trait `diesel::query_dsl::filter_dsl::FilterDsl<_>` is not implemented for `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<bool>>`
   |
   = 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<Predicate>>

error: aborting due to 3 previous errors

有人知道如何克服这个错误吗?Cargo.toml中的依赖关系:

代码语言:javascript
复制
[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" }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-11 13:16:23

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

对于柴油,这通常意味着需要导入模式,如下所示:

代码语言:javascript
复制
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))

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64786070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档