Derive Macro diesel_derives::AsChangeset

source ·
#[derive(AsChangeset)]
{
    // Attributes available to this derive:
    #[diesel]
    #[table_name]
    #[column_name]
    #[primary_key]
    #[changeset_options]
}
Expand description

Implements AsChangeset

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

If a field name of your struct differs from the name of the corresponding column, you can annotate the field with #[diesel(column_name = some_column_name)].

To provide custom serialization behavior for a field, you can use #[diesel(serialize_as = SomeType)]. If this attribute is present, Diesel will call .into on the corresponding field and serialize the instance of SomeType, rather than the actual field on your struct. This can be used to add custom behavior for a single field, or use types that are otherwise unsupported by Diesel. Normally, Diesel produces two implementations of the AsChangeset trait for your struct using this derive: one for an owned version and one for a borrowed version. Using #[diesel(serialize_as)] implies a conversion using .into which consumes the underlying value. Hence, once you use #[diesel(serialize_as)], Diesel can no longer insert borrowed versions of your struct.

By default, any Option fields on the struct are skipped if their value is None. If you would like to assign NULL to the field instead, you can annotate your struct with #[diesel(treat_none_as_null = true)].

§Attributes

§Optional container attributes

  • #[diesel(treat_none_as_null = true)], specifies that the derive should threat None values as NULL. By default Option::<T>::None is just skipped. To insert a NULL using default behavior use Option::<Option<T>>::Some(None)
  • #[diesel(table_name = path::to::table)], specifies a path to the table for which the current type is a changeset. 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.
  • #[diesel(primary_key(id1, id2))] to specify the struct field that that corresponds to the primary key. If not used, id will be assumed as primary key field

§Optional field attributes

  • #[diesel(column_name = some_column_name)], overrides the column name of the current field to some_column_name. By default the field name is used as column name.
  • #[diesel(serialize_as = SomeType)], instead of serializing the actual field type, Diesel will convert the field into SomeType using .into and serialize that instead. By default this derive will serialize directly using the actual field type.