Macro diesel::BelongsTo [] [src]

macro_rules! BelongsTo {
    (
        ($parent_struct: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)*
    ) => { ... };
    (
        (
            struct_name = $struct_name:ident,
            parent_struct = $parent_struct:ident,
            foreign_key_name = $foreign_key_name:ident,
            $($remaining_arguments:tt)*
        ),
        fields = $fields:tt,
    ) => { ... };
    (
        (
            fields = $fields:tt,
            $($remaining_args:tt)*
        ),
        found_field_with_column_name = $ignore:ident,
        field = {
            field_name: $ignore2:ident,
            column_name: $ignore3:ident,
            field_ty: $ignore4:ty,
            field_kind: $foreign_key_kind:ident,
            inner_field_ty: $foreign_key_ty:ty,
            $($rest:tt)*
        },
    ) => { ... };
    (
        (
            foreign_key_ty = $foreign_key_ty:ty,
            foreign_key_kind = regular,
            struct_name = $struct_name:ident,
            parent_struct = $parent_struct:ident,
            foreign_key_name = $foreign_key_name:ident,
            child_table_name = $child_table_name:ident,
        ),
        $($rest:tt)*
    ) => { ... };
    (
        (
            foreign_key_ty = $foreign_key_ty:ty,
            foreign_key_kind = option,
            struct_name = $struct_name:ident,
            parent_struct = $parent_struct:ident,
            foreign_key_name = $foreign_key_name:ident,
            child_table_name = $child_table_name:ident,
        ),
        $($rest:tt)*
    ) => { ... };
    (
        ($($args:tt)*)
        $struct_name:ident
        $body:tt $(;)*
    ) => { ... };
}

Defines a one-to-one association for the child table. This macro should be called with the name of the parent struct, 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,
}

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

This macro cannot be used with tuple structs.