Repository navigation
Closed
Description
Using sqlx 0.4.0-beta.1.
I have two tables in a one-to-many relation and I am trying to use query_as to retrieve them.
The tables are as follows:
CREATE TABLE IF NOT EXISTS one (
"id" INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
"name" VARCHAR NOT NULL,
)
CREATE TABLE IF NOT EXISTS many (
"id" INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
"name" VARCHAR NOT NULL,
"one" INT,
CONSTRAINT "fk_one"
FOREIGN KEY("one")
REFERENCES one
)
Then, my models in Rust are these:
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, sqlx::FromRow)]
pub struct One {
pub id: i32,
pub name: String,
pub many: Vec<Many>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, sqlx::FromRow, sqlx::Type)]
pub struct Many {
pub id: i32,
pub name: String,
}
Now, what I want to do is do a left join on these two and receive a One object with a filled many field.
Querying the database is no problem:
let rows = sqlx::query_as!(
One,
r#"
SELECT
one.id,
one.name,
(
many.id,
many.name,
) AS "many!: Vec<Many>"
FROM one
LEFT OUTER JOIN many
ON one.id = many.one
"#
)
.fetch_all(&self.pool())
.await?;
This query runs fine in psql, but... the trait sqlx::Type<sqlx::Postgres> is not implemented for std::vec::Vec<models::Many>
Is there any way to make this work with query_as!? I thought that using the sqlx::Type macro would help here but it doesn't seem to apply for vectors.
Sorry if I missed something, any pointers are much appreciated.
Reacted by Nathan Shurte, zohnannor and morzel85
Metadata
Metadata
Assignees
Labels
No labels
Activity
lyckligtax commentedon Oct 2, 2020
I have the same Problems when querying something like
where I bind a Vec<SameTypeAsTheFieldId> to $2
ekzhang commentedon Oct 2, 2020
Please correct me if I'm wrong, but is this the same as #298 ?
abonander commentedon Oct 16, 2020
@lyckligtax the
INoperator in Postgres takes a subquery, not an array. You wantid = any($2)where$2is an array.Otherwise yes, this is a duplicate of #298,