Skip to content

Commit d564f72

Browse files
committed
[SeaORM] Docs
1 parent 928037b commit d564f72

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

SeaORM/docs/06-relation/09-multi-selects.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ ORDER BY "cake"."id" ASC LIMIT 1
144144

145145
## Three-way Join
146146

147-
Our join tree starts from Order:
147+
Our join plan starts from Order:
148148

149149
```rust
150150
Order -> Customer
@@ -218,6 +218,79 @@ assert_eq!(
218218

219219
Since Cake is a related Entity of LineItem, not Order, it does not satisfy the trait bound of `left_join`. It is thus necessary to use the more flexible `join` method.
220220

221+
### Alternative shape
222+
223+
In the above, we make the nested structure resembles the topology of the join plan.
224+
But there is no restriction. Indeed, SQL flattens the select into a flat table, so as long as all columns can be found,
225+
we can freely arrange the result data structure.
226+
227+
```rust
228+
#[derive(Debug, DerivePartialModel, PartialEq)]
229+
#[sea_orm(entity = "order::Entity", from_query_result)]
230+
struct OrderItem {
231+
#[sea_orm(nested)]
232+
order: Order,
233+
#[sea_orm(nested)]
234+
customer: Customer,
235+
#[sea_orm(nested)]
236+
line: LineItem,
237+
#[sea_orm(nested)]
238+
cake: Cake,
239+
}
240+
241+
#[derive(Debug, DerivePartialModel, PartialEq)]
242+
#[sea_orm(entity = "order::Entity", from_query_result)]
243+
struct Order {
244+
#[sea_orm(from_col = "id")]
245+
order_id: i32,
246+
total: Decimal,
247+
}
248+
249+
#[derive(Debug, DerivePartialModel, PartialEq)]
250+
#[sea_orm(entity = "customer::Entity", from_query_result)]
251+
struct Customer {
252+
name: String,
253+
}
254+
255+
#[derive(Debug, DerivePartialModel, PartialEq)]
256+
#[sea_orm(entity = "lineitem::Entity", from_query_result)]
257+
struct LineItem {
258+
price: Decimal,
259+
quantity: i32,
260+
}
261+
262+
#[derive(Debug, DerivePartialModel, PartialEq)]
263+
#[sea_orm(entity = "cake::Entity", from_query_result)]
264+
struct Cake {
265+
name: String,
266+
}
267+
268+
// the exact same select query
269+
270+
assert_eq!(
271+
items,
272+
[
273+
OrderItem {
274+
order: Order {
275+
order_id: 101,
276+
total: Decimal::from(10),
277+
},
278+
customer: Customer {
279+
name: "Bob".to_owned()
280+
},
281+
line: LineItem {
282+
price: Decimal::from(2),
283+
quantity: 2,
284+
},
285+
cake: Cake {
286+
name: "Cheesecake".to_owned()
287+
},
288+
},
289+
..
290+
]
291+
);
292+
```
293+
221294
## Three-Model select
222295

223296
```rust

SeaORM/versioned_docs/version-1.1.x/06-relation/09-multi-selects.md

+73
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,79 @@ assert_eq!(
218218

219219
Since Cake is a related Entity of LineItem, not Order, it does not satisfy the trait bound of `left_join`. It is thus necessary to use the more flexible `join` method.
220220

221+
### Alternative shape
222+
223+
In the above, we make the nested structure resembles the topology of the join plan.
224+
But there is no restriction. Indeed, SQL flattens the select into a flat table, so as long as all columns can be found,
225+
we can freely arrange the result data structure.
226+
227+
```rust
228+
#[derive(Debug, DerivePartialModel, PartialEq)]
229+
#[sea_orm(entity = "order::Entity", from_query_result)]
230+
struct OrderItem {
231+
#[sea_orm(nested)]
232+
order: Order,
233+
#[sea_orm(nested)]
234+
customer: Customer,
235+
#[sea_orm(nested)]
236+
line: LineItem,
237+
#[sea_orm(nested)]
238+
cake: Cake,
239+
}
240+
241+
#[derive(Debug, DerivePartialModel, PartialEq)]
242+
#[sea_orm(entity = "order::Entity", from_query_result)]
243+
struct Order {
244+
#[sea_orm(from_col = "id")]
245+
order_id: i32,
246+
total: Decimal,
247+
}
248+
249+
#[derive(Debug, DerivePartialModel, PartialEq)]
250+
#[sea_orm(entity = "customer::Entity", from_query_result)]
251+
struct Customer {
252+
name: String,
253+
}
254+
255+
#[derive(Debug, DerivePartialModel, PartialEq)]
256+
#[sea_orm(entity = "lineitem::Entity", from_query_result)]
257+
struct LineItem {
258+
price: Decimal,
259+
quantity: i32,
260+
}
261+
262+
#[derive(Debug, DerivePartialModel, PartialEq)]
263+
#[sea_orm(entity = "cake::Entity", from_query_result)]
264+
struct Cake {
265+
name: String,
266+
}
267+
268+
// the exact same select query
269+
270+
assert_eq!(
271+
items,
272+
[
273+
OrderItem {
274+
order: Order {
275+
order_id: 101,
276+
total: Decimal::from(10),
277+
},
278+
customer: Customer {
279+
name: "Bob".to_owned()
280+
},
281+
line: LineItem {
282+
price: Decimal::from(2),
283+
quantity: 2,
284+
},
285+
cake: Cake {
286+
name: "Cheesecake".to_owned()
287+
},
288+
},
289+
..
290+
]
291+
);
292+
```
293+
221294
## Three-Model select
222295

223296
```rust

0 commit comments

Comments
 (0)