如何使用柴油机的过滤方法

时间:2020-11-11 12:02:15

标签: rust actix-web

我有一个actix网络项目。有一个这样的模型:

#[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, 柴油::查询生成器::区别_子句:: NoDistinctClause, 柴油::: builder :: whereclause :: WhereClause>: 柴油:: query_dsl :: filter_dsl :: FilterDsl << em >> is not satisfied --> src/apps/users/utils.rs:46:10 | 46 | .filter(email.eq(email)) | ^^^^^^ the trait柴油:: query_dsl :: filter_dsl :: FilterDsl << / em >> is not implemented for柴油:: query_builder :: SelectStatement | =帮助:找到以下实现: 为 柴油::: 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" }

1 个答案:

答案 0 :(得分:0)

您将<LoadScript googleMapsApiKey={process.env.NEXT_PUBLIC_GOOGLE_MAP_API_KEY} libraries={libraries} > <Autocomplete onLoad={(autocomplete) => setAutocomplete(autocomplete)} onPlaceChanged={onPlaceChanged} restrictions={{ country: "fr" }} fields={['geometry.location', 'formatted_address']} > <input type="text" placeholder="Enter your address" className="ant-input" /> </Autocomplete> </LoadScript> id与自己进行比较。您想要的是将数据库字段的值与代码中的值进行比较。

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

email

也请看一下文档中的一些实例:https://diesel.rs/guides/getting-started/


与此问题无关,您的use schema::confirmation; // .. .filter(confirmation::id.eq(id)) // .. 是否唯一?如果是这样,您还可以使用方法id,该方法允许您通过主键进行搜索。

哦,而且,您不需要两次使用find。有一种方法filter,您可以这样编写查询:.and