I have the following two structs:
#[derive(Serialize, Deserialize)]
struct Login {
email: String,
oidc_id: String
}
#[derive(Serialize, Deserialize)]
struct Account {
id: Uuid,
created: NaiveDateTime,
private_key: String,
public_key: String,
logins: Vec<Login>
}
I want to use them to read data from Postgres via Sqlx like this:
let result = sqlx::query_as!(
Account,
"select *, ARRAY(
select ROW(email, oidc_id)
from logins where logins.account_id = accounts.id
) as logins from accounts"
).fetch_all(pool).await?;
Is it possible to get this working without creating a custom record type in Postgres? I tried to implement FromRow
but Sqlx still complained about the unsupported type RECORD[]
. I also tried to derive sqlx::Type but without success.
Retrieving data like this would be very convenient, but not if I would have to declare a custom record type for each possible query.
2
Answers
From my experience with sqlx I can say that 99% of issues can be solved by type casts, including this one. From the postgres docs:
Giving it a name allows sqlx to match the type on the rust side, so that may work. The other way around is a conversion to a table type, syntax:
ROW(...)::table
:Try this: