[英]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,
柴油::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 trait
is not satisfied --> src/apps/users/utils.rs:46:10 | 46 | .filter(email.eq(email)) | ^^^^^^ the trait
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>` | = 幫助:找到以下實現:<diesel::query_builder::SelectStatement<F, S, D, W, O, L, Of, G, LC> asdiesel::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" }
您將id
以及email
與他們自己進行比較。 您想要的是將數據庫字段的值與代碼中的值進行比較。
對於柴油,這通常意味着您需要導入架構,如下所示:
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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.