You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have this common use case where I join two tables and I want to deserialize a row into separate structs. However, each row cannot be easily deserialized into separate structs via from_row.
let rows = db_conn
.query("SELECT users.*, groups.* FROM users JOIN groups ON users.group_id = groups.id WHERE users.user_id = ?1 LIMIT ?2 ",params![user_id.clone(), 10],).await?;whileletSome(row) = rows.next().await? {// there's no such thing:let(users, group) = from_row<(User,Group)>(&row)?;}
I have to do things like the following to be able to convert a Row into separate structs:
#[derive(Debug,Clone,Eq,PartialEq,Hash,Serialize,Deserialize,Default)]pubstructUserWithGroup{pubuser:User,pubgroup:Group,}// Implement from_row for UserWithGroup to deserialize from a database rowimplUserWithGroup{pubfnfrom_row<'de>(row:&'de Row) -> Result<UserWithGroup,DeError>{returnOk(Self{user:User{id: row.get(0).expect("id"),date:DateTime::deserialize(
row.get_value(1).expect("date").into_deserializer(),)?,group_id: row.get(2).expect("group_id"),},group:Group{id: row.get(3).expect("group_id"),name: row.get(4).expect("name"),},});}}
We do face the same issues. Currently, our work around for this is to use #[serde(alias = 'some_unique_name')] for each of the fields of the struct. So considering your use case, it will look something like:
And then modify your query to include those names, like so:
let rows = db_conn
.query("SELECT users.id as user_table_id, users.date as user_table_date, users.group_id as user_table_group_id, groups.id as group_table_id, groups.name as group_table_name FROM users JOIN groups ON users.group_id = groups.id WHERE users.user_id = ?1 LIMIT ?2 ",params![user_id.clone(), 10],).await?;whileletSome(row) = rows.next().await? {let user = from_row<User>(&row)?;let group = from_row<Group>(&row)?;}
Although this seems like a lot of work, but since the query is mostly generated programmatically, this is a fair bit easier to work with. But I totally agree with you that if there was some solution to this directly from libsql, it would be a huge help.
I have this common use case where I join two tables and I want to deserialize a row into separate structs. However, each row cannot be easily deserialized into separate structs via
from_row
.The query is like
I have to do things like the following to be able to convert a
Row
into separate structs:To be able to do something like
Is there a way to support
from_row
for theJOIN
use cases maybe via tuples so that this manual work is not needed anymore?The text was updated successfully, but these errors were encountered: