Macro diesel::HasMany [] [src]

macro_rules! HasMany {
    (
        ($child_table_name:ident, foreign_key = $foreign_key_name:ident)
        $($rest:tt)*
    ) => { ... };
    (
        ($($args:tt)*)
        #[table_name($table_name:ident)]
        $($rest:tt)*
    ) => { ... };
    (
        $args:tt
        $(#[$ignore:meta])*
        $(pub)* struct $($body:tt)*
    ) => { ... };
    (
        (
            parent_table_name = $parent_table_name:ident,
            child_table = $child_table:path,
            foreign_key = $foreign_key:path,
        ),
        fields = [$({
            field_name: $field_name:ident,
            column_name: $column_name:ident,
            field_ty: $field_ty:ty,
            field_kind: $field_kind:ident,
            $($rest:tt)*
        })+],
    ) => { ... };
    (
        $args:tt
        $struct_name:ident
        $body:tt $(;)*
    ) => { ... };
}

Defines a one-to-many association for the parent table. This macro is only required if you need to join between the two tables. This macro should be called with the name of the child table, followed by any options, followed by the entire struct body. The struct must be annotated with #[table_name(name_of_table)]. Both the parent and child structs must implement Identifiable.

Options

foreign_key

Required. The name of the foreign key column for this association.

Examples

pub struct User {
    id: i32,
}

pub struct Post {
    id: i32,
    user_id: i32,
}

HasMany! {
    (posts, foreign_key = user_id)
    #[table_name(users)]
    struct User {
        id: i32,
    }
}