Derive Macro diesel::prelude::Selectable

source ·
#[derive(Selectable)]
{
    // Attributes available to this derive:
    #[diesel]
}
Expand description

Implements Selectable

To implement Selectable this derive needs to know the corresponding table type. By default it uses the snake_case type name with an added s. It is possible to change this default by using #[diesel(table_name = something)].

If the name of a field on your struct is different than the column in your table! declaration, or if you are deriving this trait on a tuple struct, you can annotate the field with #[diesel(column_name = some_column)]. For tuple structs, all fields must have this annotation.

If a field is another struct which implements Selectable, instead of a column, you can annotate that struct with #[diesel(embed)]. Then all fields contained by that inner struct are selected as separate tuple. Fields from a inner struct can come from a different table, as long as the select clause is valid in current query.

The derive enables using the SelectableHelper::as_select method to construct select clauses, in order to use LoadDsl, you might also check the Queryable trait and derive.

Attributes

Type attributes

  • #[diesel(table_name = path::to::table)], specifies a path to the table for which the current type is selectable. 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.

Field attributes

  • #[diesel(column_name = some_column)], overrides the column name for a given field. If not set, the name of the field is used as column name.
  • #[diesel(embed)], specifies that the current field maps not only single database column, but is a type that implements Selectable on it’s own
  • #[diesel(select_expression = some_custom_select_expression)], overrides the entire select expression for the given field. It may be used to select with custom tuples, or specify select_expression = my_table::some_field.is_not_null(), or separate tables… It should be used in conjunction with select_expression_type (described below)
  • #[diesel(select_expression_type = the_custom_select_expression_type], to be used in conjunction with select_expression (described above). For example: #[diesel(select_expression_type = dsl::IsNotNull<my_table::some_field>)]