Macro diesel::Insertable [] [src]

macro_rules! Insertable {
    ($($args:tt)*) => { ... };
}

Implements the Insertable 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.

Example

struct NewUser<'a> {
    name: &'a str,
    hair_color: &'a str,
}

Insertable! {
    (users)
    struct NewUser<'a> {
        name: &'a str,
        hair_color: &'a str,
    }
}

To avoid copying your struct definition, you can use the custom_derive crate.

custom_derive! {
    #[derive(Insertable(users))]
    struct NewUser<'a> {
        name: &'a str,
        hair_color: &'a str,
    }
}

You can also use this macro with tuple structs, but all fields must be annotated with #[column_name(name)]. Additionally, a trailing comma after the last field is required.

struct NewUser<'a>(&'a str, Option<&'a str>);

Insertable! {
    (users)
    struct NewUser<'a>(
        #[column_name(name)]
        &'a str,
        #[column_name(hair_color)]
        Option<&'a str>,
    );
}