Skip to content
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

Codegen Handle Self Referencing & Multiple Relations to the Same Related Entity #347

Merged
merged 3 commits into from
Dec 4, 2021

Conversation

billy1624
Copy link
Member

@billy1624 billy1624 self-assigned this Dec 2, 2021
@billy1624 billy1624 requested a review from tyt2y3 December 2, 2021 08:03
@billy1624 billy1624 marked this pull request as ready for review December 2, 2021 08:39
@AngelOnFira
Copy link
Contributor

I took a quick read through the code, but not a deep dive yet. I'll try doing some exports on my databases when I get time, and see what comes up. Super excited to see this!

@billy1624
Copy link
Member Author

I took a quick read through the code, but not a deep dive yet. I'll try doing some exports on my databases when I get time, and see what comes up. Super excited to see this!

Do let us know the test results :P
Thanks!!

Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I did not author codegen. Way more complicated than I thought. Seems cool enough though.

@tyt2y3 tyt2y3 merged commit ccb8b95 into master Dec 4, 2021
@tyt2y3 tyt2y3 deleted the issues/239 branch December 4, 2021 13:22
@AngelOnFira
Copy link
Contributor

Alright, I loaded in my database, and it seems to cargo check properly. I will have to play with the ergonomics of using it, but this is very promising! Thanks for the work 😃

Here is a model with two fields that reference a wallet (some code was removed):

//! SeaORM Entity. Generated by sea-orm-codegen 0.4.0

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "currencies_transaction")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub amount: Option<i32>,
    pub from_wallet_id: Option<i64>,
    pub to_wallet_id: Option<i64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::currencies_wallet::Entity",
        from = "Column::FromWalletId",
        to = "super::currencies_wallet::Column::Id",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    CurrenciesWallet2,
    #[sea_orm(
        belongs_to = "super::currencies_wallet::Entity",
        from = "Column::ToWalletId",
        to = "super::currencies_wallet::Column::Id",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    CurrenciesWallet1,
}

impl Related<super::currencies_trade_transactions::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::CurrenciesTradeTransactions.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}

@billy1624
Copy link
Member Author

Nice!!

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.

[sea-orm-cli] Better handling of relation generations
3 participants