-
-
Notifications
You must be signed in to change notification settings - Fork 500
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
API DEMO: find_with_linked
#1725
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ pub mod common; | |
|
||
pub use chrono::offset::Utc; | ||
pub use common::{bakery_chain::*, setup::*, TestContext}; | ||
use pretty_assertions::assert_eq; | ||
pub use rust_decimal::prelude::*; | ||
pub use rust_decimal_macros::dec; | ||
use sea_orm::{entity::*, query::*, DbErr, DerivePartialModel, FromQueryResult}; | ||
|
@@ -747,6 +748,82 @@ pub async fn linked() -> Result<(), DbErr> { | |
}] | ||
); | ||
|
||
let select_baker_with_customer = Baker::find().find_with_linked(baker::BakedForCustomer); | ||
|
||
assert_eq!( | ||
select_baker_with_customer | ||
.build(sea_orm::DatabaseBackend::MySql) | ||
.to_string(), | ||
[ | ||
// FIXME: This might be faulty! | ||
"SELECT `baker`.`id` AS `A_id`,", | ||
"`baker`.`name` AS `A_name`,", | ||
"`baker`.`contact_details` AS `A_contact_details`,", | ||
"`baker`.`bakery_id` AS `A_bakery_id`,", | ||
"`r4`.`id` AS `B_id`,", | ||
"`r4`.`name` AS `B_name`,", | ||
"`r4`.`notes` AS `B_notes`", | ||
"FROM `baker`", | ||
"INNER JOIN `cakes_bakers` AS `r0` ON `r0`.`baker_id` = `baker`.`id`", | ||
"INNER JOIN `cake` AS `r1` ON `r1`.`id` = `r0`.`cake_id`", | ||
"INNER JOIN `lineitem` AS `r2` ON `r2`.`cake_id` = `r1`.`id`", | ||
"INNER JOIN `order` AS `r3` ON `r3`.`id` = `r2`.`order_id`", | ||
"INNER JOIN `customer` AS `r4` ON `r4`.`id` = `r3`.`customer_id`", | ||
"ORDER BY `baker`.`id` ASC", | ||
] | ||
.join(" ") | ||
); | ||
|
||
assert_eq!( | ||
select_baker_with_customer.all(&ctx.db).await?, | ||
[ | ||
( | ||
baker::Model { | ||
id: 1, | ||
name: "Baker Bob".into(), | ||
contact_details: serde_json::json!({ | ||
"mobile": "+61424000000", | ||
"home": "0395555555", | ||
"address": "12 Test St, Testville, Vic, Australia", | ||
}), | ||
bakery_id: Some(1), | ||
}, | ||
vec![customer::Model { | ||
id: 2, | ||
name: "Kara".into(), | ||
notes: Some("Loves all cakes".into()), | ||
}] | ||
), | ||
( | ||
baker::Model { | ||
id: 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this should be a 2 instead of 1? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems this is a follow up question of #1725 (comment)? |
||
name: "Baker Bobby".into(), | ||
contact_details: serde_json::json!({ | ||
"mobile": "+85212345678", | ||
}), | ||
bakery_id: Some(1), | ||
}, | ||
vec![ | ||
customer::Model { | ||
id: 1, | ||
name: "Kate".into(), | ||
notes: Some("Loves cheese cake".into()), | ||
}, | ||
customer::Model { | ||
id: 1, | ||
name: "Kate".into(), | ||
notes: Some("Loves cheese cake".into()), | ||
}, | ||
customer::Model { | ||
id: 2, | ||
name: "Kara".into(), | ||
notes: Some("Loves all cakes".into()), | ||
Comment on lines
+807
to
+820
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if the order of these matters or not, since they return in reverse order for my implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that I have have ORDER BY `baker`.`id` ASC |
||
}, | ||
] | ||
), | ||
] | ||
); | ||
|
||
ctx.delete().await; | ||
|
||
Ok(()) | ||
|
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.
Should this be a inner join or a left join?
I found that other find_with/find_also functions use left join instead of inner join
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.
Yes, it should be
LEFT JOIN