回答編集履歴

2

削除

2024/09/11 03:08

投稿

bsdfan
bsdfan

スコア4749

test CHANGED
@@ -1,6 +1,3 @@
1
- - https://www.sea-ql.org/SeaORM/docs/advanced-query/custom-select/#handling-select-results
2
- + FromQueryResult をderiveたstructをつくって`into_model()`する
1
+ (質問者からの反応がないため削除ます)
3
- + `into_tuple()` する (tuple明示する必要あり)
2
+ 公式ドキュメント読めばちゃんと書いてあります。
4
3
 
5
- - https://www.sea-ql.org/SeaORM/docs/basic-crud/select/#find-related-models
6
- + `find_also_related()` を使う

1

回答の不要な部分を削除

2024/09/01 23:35

投稿

bsdfan
bsdfan

スコア4749

test CHANGED
@@ -1,35 +1,6 @@
1
- `employees::Entity::find()`からスタートして、特に変換もせず `all()` しているので、結果で出てくるのは `employees::Model`の配列になるのではないでしょうか。
2
- 公式の別のページ https://www.sea-ql.org/SeaORM/docs/advanced-query/custom-select/#handling-select-results をみると、結果を受け取るための FromQueryResult をderiveしたstructをつくって`into_model()`すればよさそうです。
1
+ - https://www.sea-ql.org/SeaORM/docs/advanced-query/custom-select/#handling-select-results
3
- ```Rust
2
+ + FromQueryResult をderiveしたstructをつくって`into_model()`する
4
- #[derive(FromQueryResult, Debug)]
3
+ + `into_tuple()` する (tupleの型を明示する必要あり)
5
- struct EmployeeAndDepartment {
6
- id: i32,
7
- name: String,
8
- department_id: i32,
9
- department_name: String,
10
- }
11
4
 
12
-
13
- pub async fn select_employees_join(db: &DbConn) -> Result<Vec<EmployeeAndDepartment>, sea_orm::DbErr> {
14
- employees::Entity::find()
15
- .inner_join(departments::Entity)
16
- .column_as(departments::Column::Name, "department_name")
17
- .into_model::<EmployeeAndDepartment>()
18
- .all(db)
19
- .await
20
- }
21
- ```
22
-
23
- もしくは `into_tuple()` してtupleで受け取ってもよさそうです。
24
- ```Rust
25
- pub async fn select_employees_join(db: &DbConn) -> Result<Vec<(i32, String, i32, String)>, sea_orm::DbErr> {
26
- employees::Entity::find()
27
- .inner_join(departments::Entity)
28
- .column(departments::Column::Name)
29
- .into_tuple()
30
- .all(db)
31
- .await
32
- }
33
- ```
34
- あとは、やりたいこと次第ですが `find_also_related()` を使うのでもいいんじゃないかと思いました。
35
- https://www.sea-ql.org/SeaORM/docs/basic-crud/select/#find-related-models
5
+ - https://www.sea-ql.org/SeaORM/docs/basic-crud/select/#find-related-models
6
+ + `find_also_related()` を使う