#3189·sea-orm

[EntityLoader] `EntityLoader::load()::with()` 对 `self_ref` 自引用不起作用。

作者: leuchthelp创建于 2026年8月31日更新于 2026年9月6日
标签Area:docsCategory:enhancementhelp-wanted

use sea_orm::entity::prelude::*;

#[sea_orm::model] #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] #[sea_orm(table_name = "humans")] pub struct Model { #[sea_orm(primary_key)] id: i32, #[sea_orm(has_many)] pub hats: HasManysuper::hats::Entity, #[sea_orm( self_ref, via = "jt_parent_to_child", from = "Humans", to = "Child" )] pub children: HasMany, #[sea_orm(self_ref, via = "jt_parent_to_child", reverse)] pub parents: HasMany, }

impl ActiveModelBehavior for ActiveModel {}


jt_parent_child.rs
```rs
use sea_orm::entity::prelude::*;

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "jt_parent_to_child")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub parent_id: i32,
    #[sea_orm(primary_key, auto_increment = false)]
    pub child_id: i32,
    #[sea_orm(belongs_to, from = "parent_id", to = "id")]
    pub parent: BelongsTo<super::humans::Entity>,
    #[sea_orm(belongs_to, relation_enum = "Child", from = "child_id", to = "id")]
    pub child: BelongsTo<super::humans::Entity>,
}

impl ActiveModelBehavior for ActiveModel {}