I'm trying to load a table with all related tables joined in rust with seaOrm. After trying for a while I managed to write a select statement which seem to load all the tables as requested. However I can't get them in a struct or json format as desired. When using into_json() all data is displayed but for every change a new row is generated (like in SQL).
let data = Sale::find_by_id(1)
.join(JoinType::InnerJoin, sale::Relation::Product.def())
.columns(product::Column::iter())
.join(JoinType::InnerJoin, product::Relation::ProductPart.def())
.columns(product_part::Column::iter())
.join(JoinType::InnerJoin, sale::Relation::Seller.def())
.columns(seller::Column::iter())
.into_json()
.all(&db)
.await?;
- Each sale has multiple
Product - Each product has multiple
ProductParts - Each sale has one
Seller
If I just join Sale and Seller I get 1 line of result. As soon as I join Product I get a few multiple lines which are again multiplied if joined with ProductPart for each ProductPart.
Another problem here seems to be that when the same column name in different tables exist this is overwritten. I've name both in Sale and Product and the name key in the json gets the value of the Product name field.
How can I get this is in some kind of stuct or json format like this:
#[derive(FromQueryResult, Debug)]
struct SaveStruct {
sale_uid: Uuid,
name: Option<String>,
products: Vec<Product>,
seller: Seller,
}
struct Product{
uid: Uuid,
name: Option<String>,
parts: Vec<ProductPart>,
}
struct Seller{
name: Option<String>,
}
Or even better in json like this:
{
"sale_uid": 1,
"name": "sale_name",
"products": [
{
"id": "1",
"name": "Product 1",
"parts": [
{
"type": "type",
"id": 0
},
{
"type": "type",
"id": 1
},
]
}
],
"seller": {
"name": "name",
}
}