Skip to content

deep nest load with ModelEx #2942

@chungwong

Description

@chungwong

Motivation

With the following self-referencing model

#[sea_orm::model]
#[derive(Clone, Debug, DeriveEntityModel, Deserialize, Eq, PartialEq, Serialize)]
#[sea_orm(table_name = "extractor")]
#[non_exhaustive]
struct Model {
    #[sea_orm(primary_key)]
    id: i64,
    #[sea_orm(unique_key = "unique_selector")]
    parent_id: Option<i64>,
    #[sea_orm(
        self_ref,
        from = "parent_id",
        relation_enum = "Parent",
        relation_reverse = "Children",
        to = "id"
    )]
    parent: HasOne<Entity>,
    #[sea_orm(self_ref, relation_enum = "Children", relation_reverse = "Parent")]
    children: HasMany<Entity>,
    #[sea_orm(unique_key = "unique_selector")]
    website_id: i64,
    #[sea_orm(belongs_to, from = "website_id", to = "id")]
    website: HasOne<super::website::Entity>,
}

it can be loaded as

extractor::Entity::load()
.with(extractor::Relation::Children) // loading the children
// .with((extractor::Relation::Children, extractor::Relation::Website)) this doesn't work, see the error at the end.
.with(website::Entity)
.filter(website::Column::Id.eq(website_id))
.filter(extractor::Column::ParentId.is_null())
.all(&state.db)
.await

However, it does not load website for the children

ModelEx {
    id: 8,
    parent_id: None,
    parent: Unloaded,
    children: Loaded(
        [
            ModelEx {
                id: 9,
                parent_id: Some(
                    8,
                ),
                parent: Unloaded,
                children: Unloaded,
                website_id: 1,
                website: Unloaded, // website is not loaded in the second/child level
            },
        ],
    ),
    website_id: 1,
    website: Loaded( // <<< website is loaded on the first/parent level
        ModelEx {
            id: 1,
            name: "",
            url: "",
            extractors: Unloaded,
        },
    ),
}

Additional Information

Discord
Probably a similar question

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions