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

Support nested entities #2508

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Support nested entities #2508

wants to merge 9 commits into from

Conversation

tyt2y3
Copy link
Member

@tyt2y3 tyt2y3 commented Feb 23, 2025

Cont'd #2179

feat: allow for recursive usage of FromQueryResult derive

fix: fix tests to maintain API (even though probably should not..)

fix: do not compile informative code snippet

feat: allow nested attribute in DerivePartialModel

feat: test cases and fixes for DerivePartialModel

feat: enable DerivePartialModel for Option<T>

fix: fix typos

feat: update documentation

fix: fix comment

fix: remove unused import

feat: add test and some fixes regarding option

feat: properly differentiate same-name columns via explicit AS

fix: cargo fmt

fix: more formatting

feat: more tests

fix: reorganize tests

fix: use - instead of _ to separate identifiers to avoid mixups

fix: add comment explaining error handling in FromQueryResult derive

feat: add aspirational test

fix: remove eprintln

chore: clippy
@tyt2y3
Copy link
Member Author

tyt2y3 commented Feb 23, 2025

@jreppnow I rebased. seems like all tests are passing. I am still trying to reconcile the conflict on format!("{pre}{}-"

@tyt2y3
Copy link
Member Author

tyt2y3 commented Feb 23, 2025

@Sytten @jreppnow thank you both of your contributions!

I think we've came to a solution. In summary:

  1. I've added from_alias to FromQueryResult
  2. FromQueryResult don't put prefix by default
  3. DerivePartialModel can still work with FromQueryResult like before, so we're not breaking anyone
  4. Added tag from_query_result to DerivePartialModel, which will derive FromQueryResult under the hood with prefix. this is the only way that nested could work
#[derive(DerivePartialModel)]
#[sea_orm(entity = "cake::Entity", from_query_result)]
struct Cake {
    id: i32,
    name: String,
    #[sea_orm(nested)]
    bakery: Option<Bakery>,
}

Comment on lines 10 to 25
#[derive(FromQueryResult)]
#[sea_orm(entity = "cake::Entity")]
struct Cake {
id: i32,
name: String,
#[sea_orm(nested)]
bakery: Option<CakeBakery>,
}

#[derive(FromQueryResult)]
struct CakeBakery {
#[sea_orm(from_alias = "bakery_id")]
id: i32,
#[sea_orm(from_alias = "bakery_name")]
title: String,
}
Copy link
Member Author

Choose a reason for hiding this comment

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

FromQueryResult alone

Comment on lines +105 to +122
let cake: Cake = cake::Entity::find()
.select_only()
.column(cake::Column::Id)
.column(cake::Column::Name)
.column_as(bakery::Column::Id, "bakery_id")
.column_as(bakery::Column::Name, "bakery_name")
.left_join(bakery::Entity)
.into_model()
.one(&ctx.db)
.await
.expect("succeeds to get the result")
.expect("exactly one model in DB");

assert_eq!(cake.id, 13);
assert_eq!(cake.name, "Test Cake");
let bakery = cake.bakery.unwrap();
assert_eq!(bakery.id, 42);
assert_eq!(bakery.title, "cool little bakery");
Copy link
Member Author

Choose a reason for hiding this comment

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

test

Comment on lines +76 to +93
#[derive(FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "bakery::Entity")]
struct Bakery {
id: i32,
#[sea_orm(from_col = "Name")]
title: String,
}

#[derive(DerivePartialModel)]
#[sea_orm(entity = "cake::Entity", from_query_result)]
struct Cake {
id: i32,
name: String,
#[sea_orm(nested)]
bakery: Option<Bakery>,
#[sea_orm(skip)]
ignore: Ignore,
}
Copy link
Member Author

Choose a reason for hiding this comment

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

DerivePartialModel + FromQueryResult

Comment on lines +159 to +170
let cake: Cake = cake::Entity::find()
.left_join(bakery::Entity)
.into_partial_model()
.one(&ctx.db)
.await
.expect("succeeds to get the result")
.expect("exactly one model in DB");

assert_eq!(cake.id, 13);
assert_eq!(cake.name, "Test Cake");
assert!(matches!(cake.bakery, Some(Bakery { id: 42, .. })));
assert_eq!(cake.bakery.unwrap().title, "cool little bakery");
Copy link
Member Author

Choose a reason for hiding this comment

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

test

@tyt2y3
Copy link
Member Author

tyt2y3 commented Feb 23, 2025

would appreciate if you guys can review and add some test cases

@tyt2y3 tyt2y3 changed the title Support nested entities (#2507) Support nested entities Feb 24, 2025
@tyt2y3
Copy link
Member Author

tyt2y3 commented Feb 25, 2025

@Sytten works for you?

@Sytten
Copy link
Contributor

Sytten commented Feb 25, 2025

Yeah works for me, sorry I am sick so didnt have chance to look into more details

@tyt2y3
Copy link
Member Author

tyt2y3 commented Feb 25, 2025

Sytten, thank you, and take care!

Comment on lines +78 to +84
#[derive(FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "bakery::Entity")]
struct Bakery {
id: i32,
#[sea_orm(from_col = "Name")]
title: String,
}
Copy link
Member Author

@tyt2y3 tyt2y3 Feb 25, 2025

Choose a reason for hiding this comment

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

@Goodjooy since you're also a contributor. I realised we've made a small breaking change here, the from_col expression changed from snake_cake to TypeCase, corresponding to the variants of the Column enum directly. may be this is more consistent from a design sense? because this will work even when the entity has the column name re-mapped.

what's your view?

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.

3 participants