Andrea Proone
Andrea Proone

Reputation: 94

How to update an existing object with Diesel? "`T: Identifiable` is not satisfied"

I have this struct:

#[derive(Identifiable, Queryable, Debug)]
#[table_name = "users"]
pub struct QueryableUser {
    pub id: i32,
    pub username: String,
    pub password: String,
    pub sex: bool,
    pub profile: Option<String>,
    pub birth: chrono::NaiveDate,
}

When I try to update the struct like so:

diesel::update(&queryable_user).set(...);

it gives me this error:

error[E0277]: the trait bound `user::QueryableUser: diesel::Identifiable` is not satisfied
  --> src\message_handler\set_profile.rs:36:13
   |
36 |             diesel::update(user).set(profile.eq(None));
   |             ^^^^^^^^^^^^^^ the trait `diesel::Identifiable` is not implemented for `user::QueryableUser`
   |
   = help: the following implementations were found:
             <&'ident user::QueryableUser as diesel::Identifiable>
   = note: required because of the requirements on the impl of `diesel::query_builder::IntoUpdateTarget` for `user::QueryableUser`
   = note: required by `diesel::update`

It's really confusing because I used #[derive(Identifiable)] on my struct.

Upvotes: 2

Views: 663

Answers (1)

Andrea Proone
Andrea Proone

Reputation: 94

From the documentation on Identifiable:

This must be implemented to use associations. Additionally, implementing this trait allows you to pass your struct to update (update(&your_struct) is equivalent to update(YourStruct::table().find(&your_struct.primary_key())).

This trait is usually implemented on a reference to a struct, not the struct itself.

So you simply need to borrow user:

diesel::update(&user).set(...);

Upvotes: 2

Related Questions