Skip to main content

BelongsTo

Trait BelongsTo 

Source
pub trait BelongsTo<T: Entity>: Entity {
    // Required method
    fn foreign_key() -> &'static str;

    // Provided method
    fn owner_key() -> &'static str { ... }
}
Expand description

一对多关系的反向 trait

定义从实体与主实体的多对一关系

§Example

struct Post {
    id: i64,
    user_id: i64,
    user: Option<User>,
}

impl BelongsTo<User> for Post {
    fn foreign_key() -> &'static str {
        "user_id"
    }

    fn owner_key() -> &'static str {
        "id"
    }
}

Required Methods§

Source

fn foreign_key() -> &'static str

外键列名(当前表中的列名)

Provided Methods§

Source

fn owner_key() -> &'static str

拥有者键列名(主表中的列名),默认为主键列名

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§