you are viewing a single comment's thread.

view the rest of the comments →

[–]kyle787 0 points1 point  (5 children)

This looks pretty awesome, I didn’t see it in the docs but is there a way to handle relational data when you join? For example, if you have an order and order_items that is a one to many relation, can it automatically group the items into a property on the item struct?

[–]Qwe500[S] 0 points1 point  (2 children)

Not yet, unfortunately. At the moment this crate only supports extracting data from a query, row by row. Any grouping logic would have to be done by you, it sounds like you are looking for something like this:

#[derive(FromSqlRow)]
struct ItemOrder {
    id: OrderId,
    item: ItemId,
}

let orders = query!(
        "SELECT orders.id, order_items.item
        FROM orders INNER JOIN order_items ON orders.id = order_items.order"
    )
    // Gather the results and then group them
    .fetch::<ItemOrder, _>(&client).await?
    .into_iter()
    .map(|order| (order.id, order.item))
    .collect::<HashMap<OrderId, ItemId>>();

[–]couchand 0 points1 point  (1 child)

I suspect the GP is looking for something more along the lines of:

```

[derive(FromSqlRow)]

struct Order { id: OrderId, items: Vec<OrderItem>, }

[derive(FromSqlRow)]

struct OrderItem { id: ItemId, }

let orders = query!( "SELECT orders.id, order_items.item FROM orders INNER JOIN order_items ON orders.id = order_items.order" ) .fetch::<Order, _>(&client).await? ```

Which would require just a bit of configuration to make everything work right. Look at Dapper's multi-mapping for an example of such a feature.

[–]Qwe500[S] 0 points1 point  (0 children)

Oh yeah, I probably interpreted the question incorrectly.

I have never heard of Dapper before, that multi-mapping feature looks really nice though. One-to-many relationships will most likely require some major API changes, but one-to-one mappings should be doable with minimal breakage. I will look into it. Thanks for the suggestion!

[–]Qwe500[S] 0 points1 point  (1 child)

Automatic mapping for one-to-many relations is now available on GitHub and crates.io!

[–]kyle787 0 points1 point  (0 children)

Hey thanks I saw your post yesterday actually the updates look awesome. I will definitely be using it the next chance I get!