Derive Macro diesel::expression::Selectable

source ·
    // Attributes available to this derive:
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 from the column in your table! declaration, or if you’re 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 with #[diesel(embed)]. Then all fields contained by that inner struct are selected as separate tuple. Fields from an inner struct can come from a different table, as long as the select clause is valid in the 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.


§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.

§Optional Type attributes

  • #[diesel(check_for_backend(diesel::pg::Pg, diesel::mysql::Mysql))], instructs the derive to generate additional code to identify potential type mismatches. It accepts a list of backend types to check the types against. Using this option will result in much better error messages in cases where some types in your Queryable struct don’t match. You need to specify the concrete database backend this specific struct is indented to be used with, as otherwise rustc can’t correctly identify the required deserialization implementation.

§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 a single database column, but is a type that implements Selectable on its 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>)]