+
Skip to content

feta: Allow DerivePartialModel to add nested aliases and associate the same model in the model. #2639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 1.1.x
Choose a base branch
from

Conversation

Silence-kzp
Copy link

PR Info

Hello, this is the implementation of #2636. The same PartialModel cannot be nested in PartialModel. Add the alias attribute and implement [alias].id as alias_id when selecting.

Usage

let query = crate::bill_records::Entity::find()
        .join_as(sea_orm::JoinType::LeftJoin, super::bill_records::Relation::Depositor.def(), "depositor")
        .join_as(sea_orm::JoinType::LeftJoin, super::bill_records::Relation::Recorder.def(), "recorder")
        .into_partial_model::<super::bill_records::BillRecord>()
        .all(db)
        .await?;

Model file definition:

bill_record.rs
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name="bill_records")]
pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i64,
       // recorder
        #[sea_orm(nested, alias="recorder")]
        pub recorder_id: Option<i64>,
       // depositor
        #[sea_orm(nested, alias="depositor")]
        pub depositor_id: Option<i64>,
}

#[derive(Debug, Clone, Deserialize, Serialize, DerivePartialModel)]
#[sea_orm(entity = "Entity", from_query_result, into_active_model)]
pub struct BillRecord {
       pub id: i64,
       // recorder
        pub recorder: Option<super::member::Member>,
       // depositor
        pub depositor: Option<super::member::Member>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to="super::models::member::Entity",
        from = "Column::DepositorId",
        to = "super::models::member::Column::Id"
    )]
    Depositor,
    #[sea_orm(
        belongs_to="super::models::member::Entity",
        from = "Column::RecorderId",
        to = "super::models::member::Column::Id"
    )]
    Recorder,
}
member.rs
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name="members")]
pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i64,
        pub r#type: MemberType,
        pub name: String,
        pub username: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, DerivePartialModel)]
#[sea_orm(entity = "Entity", from_query_result, into_active_model)]
pub struct Member {
       pub id: i64,
       pub r#type: MemberType,
       pub name: String,
       pub username: String,
}

#[derive(Debug, Clone,  Deserialize, Serialize, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(10))")]
#[serde(rename_all = "lowercase")]
pub enum MemberType {
    #[sea_orm(string_value = "unknow")]
    Unknow,

    #[sea_orm(string_value = "depositor")]
    Depositor,

    #[sea_orm(string_value = "recorder")]
    Recorder,
}

impl IntoActiveValue<MemberType> for MemberType {
    fn into_active_value(self) -> ActiveValue<MemberType> {
        ActiveValue::Set(self)
    }
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many= "super::bill_records::Entity")]
    Depositor,
    #[sea_orm(has_many= "super::bill_records::Entity")]
    Recorder,
}

@tyt2y3
Copy link
Member

tyt2y3 commented Jul 1, 2025

Thank you for you contribution! that seems smart, can you add some test cases in https://github.com/SeaQL/sea-orm/blob/1.1.x/tests/partial_model_tests.rs ?

@Silence-kzp
Copy link
Author

Thank you for you contribution! that seems smart, can you add some test cases in https://github.com/SeaQL/sea-orm/blob/1.1.x/tests/partial_model_tests.rs ?

I have added the corresponding test case. (But it doesn't feel like a very good test case)

@tyt2y3
Copy link
Member

tyt2y3 commented Jul 3, 2025

the test cases are very good indeed. and it's a smart implementation.
although I'd say instead of changing bakery, may be it's easier to just create a new entity e.g. Factory + Worker = FactoryWorker? or a Icecream + Fruit?

I will merge this once CI passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载