Macro diesel::impl_AsChangeset [] [src]

macro_rules! impl_AsChangeset {
    (
        ($table_name:ident)
        $($body:tt)*
    ) => { ... };
    (
        $args:tt
        $(#[$ignore:meta])*
        $(pub)* struct $($body:tt)*
    ) => { ... };
    (
        ($table_name:ident, treat_none_as_null=$treat_none_as_null:tt)
        $struct_name:ident <$($lifetime:tt),*>
        $body:tt $(;)*
    ) => { ... };
    (
        ($table_name:ident, treat_none_as_null=$treat_none_as_null:tt)
        $struct_name:ident
        $body:tt $(;)*
    ) => { ... };
    (
        (
            struct_name = $struct_name:ident,
            table_name = $table_name:ident,
            treat_none_as_null = $treat_none_as_null:tt,
            $($headers:tt)*
        ),
        fields = [$($field:tt)+],
    ) => { ... };
    (
        (
            fields = [$({
                column_name: $column_name:ident,
                field_ty: $field_ty:ty,
                field_kind: $field_kind:ident,
                $($rest:tt)*
            })+],
            struct_name = $struct_name:ident,
            $($headers:tt)*
        ),
        changeset_ty = $changeset_ty:ty,
    ) => { ... };
    (
        (
            fields = [$({
                field_name: $field_name:ident,
                column_name: $column_name:ident,
                field_ty: $field_ty:ty,
                field_kind: $field_kind:ident,
                $($rest:tt)*
            })+],
            struct_name = $struct_name:ident,
            $($headers:tt)*
        ),
        changeset_ty = $changeset_ty:ty,
    ) => { ... };
    (
        table_name = $table_name:ident,
        treat_none_as_null = $treat_none_as_null:tt,
        struct_ty = $struct_ty:ty,
        lifetimes = ($($lifetime:tt),*),
        self_to_columns = $self_to_columns:pat,
        columns = ($($column_name:ident, $field_kind:ident),+),
        field_names = $field_names:tt,
        changeset_ty = $changeset_ty:ty,
    ) => { ... };
}

Implements the AsChangeset trait for a given struct. This macro should be called with the name of the table you wish to use the struct with, followed by the entire struct body. This macro mirrors #[as_changeset] from diesel_codegen

Options

  • treat_none_as_null (boolean)
    • Default value: "false"
    • When set to "true", option fields will set the column to NULL when their value is None. When set to "false", the field will not be assigned.

Example


#[derive(PartialEq, Debug, Queryable)]
struct User {
    id: i32,
    name: String,
}

impl_AsChangeset! {
    (users)
    struct User {
        id: i32,
        name: String,
    }
}



diesel::insert(&NewUser::new("Sean"))
    .into(users)
    .execute(&connection)
    .unwrap();
let user_id = users.select(id).order(id.desc()).first(&connection).unwrap();
let changes = User::new(user_id, "Jim");
diesel::update(users.find(user_id))
    .set(&changes)
    .execute(&connection)
    .unwrap();

let user_in_db = users.find(user_id).first(&connection);
assert_eq!(Ok(changes), user_in_db);