Trait diesel::associations::Identifiable [−][src]
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 the struct itself.
Deriving
This trait can be automatically derived by adding #[derive(Identifiable)]
to your struct.
By default, the "id" field is assumed to be a single field called id
.
If it's not, you can put #[primary_key(your_id)]
on your struct.
If you have a composite primary key, the syntax is #[primary_key(id1, id2)]
.
By default, #[derive(Identifiable)]
will assume that your table
name is the plural form of your struct name.
Diesel uses very simple pluralization rules.
It only adds an s
to the end, and converts CamelCase
to snake_case
.
If your table name does not follow this convention
or the plural form isn't just an s
,
you can specify the table name with #[table_name = "some_table_name"]
.
Our rules for inferring table names is considered public API.
It will never change without a major version bump.
Associated Types
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
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
.