diesel::associations

Derive Macro Associations

Source
#[derive(Associations)]
{
    // Attributes available to this derive:
    #[diesel]
    #[belongs_to]
    #[column_name]
    #[table_name]
}
Expand description

Implement required traits for the associations API

This derive implements support for Diesel’s associations api. Check the module level documentation of the diesel::associations module for details.

This derive generates the following impls:

  • impl BelongsTo<Parent> for YourType
  • impl BelongsTo<&'a Parent> for YourType

§Attributes

§Required container attributes

  • #[diesel(belongs_to(User))], to specify a child-to-parent relationship between the current type and the specified parent type (User). If this attribute is given multiple times, multiple relationships are generated. #[diesel(belongs_to(User, foreign_key = mykey))] variant allows us to specify the name of the foreign key. If the foreign key is not specified explicitly, the remote lower case type name with appended _id is used as a foreign key name. (user_id in this example case)

§Optional container attributes

  • #[diesel(table_name = path::to::table)] specifies a path to the table this type belongs to. The path is relative to the current module. If this attribute is not used, the type name converted to snake_case with an added s is used as table name.

§Optional field attributes

  • #[diesel(column_name = some_column_name)], overrides the column the current field maps to some_column_name. By default, the field name is used as a column name.