nocl1p
nocl1p

Reputation: 121

How to use diesel's filter methods?

I have a little actix web project. There is a such model:

#[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
}

Then I have a function where I just wanna get item from the database. I try to do this:

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)
}

And finally I got this error:

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

Does anybody know how to fight this error? Dependencies in 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" }

Upvotes: 0

Views: 3119

Answers (1)

rethab
rethab

Reputation: 8413

Your comparing the id as well as the email to themselves. What you want is to compare the database field's value to the value in your code.

For diesel, this typically means you need to import your schema, like so:

use schema::confirmation;

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

Also take a look at the docs for some exaples: https://diesel.rs/guides/getting-started/


Unrelated to this problem, isn't your id unique? If so, you could also use the method find which allows you to search by primary key.

Oh, and also, you don't need to use filter twice. There's a method .and which allows you to write a query like so: schema::id.eq(id).and(schema::email.eq(email))

Upvotes: 3

Related Questions