postgresql - 如何使用 Diesel 更新现有对象? "` T : Identifiable` is not satisfied"

标签 postgresql rust rust-diesel

我有这个结构:

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

当我尝试像这样更新结构时:

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

它给了我这个错误:

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`

这真的很令人困惑,因为我在我的结构上使用了#[derive(Identifying)]

最佳答案

来自 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.

所以你只需要借用用户:

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

关于postgresql - 如何使用 Diesel 更新现有对象? "` T : Identifiable` is not satisfied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57747246/

相关文章:

rust - 如何使用 Diesel 编写多个显式的 inner_join?

sql - Oracle Select 语法到 Postgres

sql - 如何在 SQL 中迭代所有名称和排名排列

rust - 如何使用 Tokio 实现基于拉取的系统?

rust - 泛型函数的不同实例是否可以有不同的静态变量?

rust - 如何使用 Diesel 的 Pg 数字类型插入小数?

sql - 带有要记录的数组的 Json 列

python - sqlalchemy.exc.NoReferencedTableError : Foreign key associated with column X could not find table Y with which to generate a foreign key 错误

rust - Cargo 将 git 需求放在哪里?

mysql - 使用 Diesel 的声明式架构定义