Trait diesel::associations::Identifiable

source ·
pub trait Identifiable: HasTable {
    type Id: Hash + Eq;

    // Required method
    fn id(self) -> Self::Id;
}
Expand description

This trait indicates that a struct represents a single row in a database table.

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 on the struct itself. It can be derived.

Required Associated Types§

source

type Id: Hash + Eq

The type of this struct’s identifier.

For single-field primary keys, this is typically &'a i32, or &'a String For composite primary keys, this is typically (&'a i32, &'a i32) or (&'a String, &'a String), etc.

Required Methods§

source

fn id(self) -> Self::Id

Returns the identifier for this record.

This takes self by value, not reference. This is because composite primary keys are typically stored as multiple fields. We could not return &(String, String) if each string is a separate field.

Because of Rust’s rules about specifying lifetimes, this means that Identifiable is usually implemented on references so that we have a lifetime to use for Id.

Object Safety§

This trait is not object safe.

Implementors§