-
-
Notifications
You must be signed in to change notification settings - Fork 550
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
base: master
Are you sure you want to change the base?
Support nested entities #2508
Conversation
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
@jreppnow I rebased. seems like all tests are passing. I am still trying to reconcile the conflict on |
@Sytten @jreppnow thank you both of your contributions! I think we've came to a solution. In summary:
#[derive(DerivePartialModel)]
#[sea_orm(entity = "cake::Entity", from_query_result)]
struct Cake {
id: i32,
name: String,
#[sea_orm(nested)]
bakery: Option<Bakery>,
} |
tests/from_query_result_tests.rs
Outdated
#[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, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FromQueryResult
alone
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test
#[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, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DerivePartialModel
+ FromQueryResult
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test
would appreciate if you guys can review and add some test cases |
@Sytten works for you? |
Yeah works for me, sorry I am sick so didnt have chance to look into more details |
Sytten, thank you, and take care! |
#[derive(FromQueryResult, DerivePartialModel)] | ||
#[sea_orm(entity = "bakery::Entity")] | ||
struct Bakery { | ||
id: i32, | ||
#[sea_orm(from_col = "Name")] | ||
title: String, | ||
} |
There was a problem hiding this comment.
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?
Cont'd #2179