Generic update #2969
-
Hi everyone! My generic update code: fn update(&self, item: T) -> Result<T>
where
T: IntoUpdateTarget + AsChangeset<Target = T::Table>,
Update<T, T>: LoadQuery<DbPooledConnection, T>,
{
Ok(diesel::update(item.clone())
.set(item)
.get_result(&self.get_connection()?)?)
} An example model: #[derive(
AsChangeset,
Clone,
Debug,
Deserialize,
Identifiable,
Insertable,
Queryable,
Serialize,
)]
pub struct Service {
#[serde(skip)]
pub id: KeyType,
pub title: String,
pub description: String,
pub available_begin: Option<chrono::NaiveTime>,
pub available_end: Option<chrono::NaiveTime>,
} The error I get when calling the update function: 137 | self.ctx.services().update(service)?;
| ^^^^^^ the trait `diesel::associations::Identifiable` is not implemented for `Service` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This error is expected as impl<'a> Identifiable for &'a Service {
// …
} As the corresponding As your code example is extremely incomplete it's hard to exactly tell how to solve this. You can either try to add an manual impl of |
Beta Was this translation helpful? Give feedback.
This error is expected as
#[derive(Identifiable)]
generates the following impl:As the corresponding
IntoUpdateTarget
impl assumes that the type implementsIdentifiable
this implies that the type must be a reference (or you must provide a manual impl ofIdentifiable
for your type itself.) In concequence this means that the boundT: IntoUpdateTarget + AsChangeset<Target = T::Table>
is not fulfilled.As your code example is extremely incomplete it's hard to exactly tell how to solve this. You can either try to add an manual impl of
Identifiable
to yourService
struct or try to adjust that trait bound so that at least theIntoUpdateTarget
part …