1:N issue #1451
Replies: 1 comment
-
SQLx doesn't inherently support 1:N queries like this, as that actually takes more than one query to accomplish most of the time and SQLx isn't that implicit. One option, if you're fetching a single let patient = sqlx::query_as!(
Patient,
r#"
SELECT id, patient_id, patient_name, NULL::string[] as "studies: Vec<Study>
FROM patients
...
"#
)
.fetch_one(&mut conn)
.await?;
patient.studies = Some(sqlx::query_as!(Study, "SELECT ...").fetch_all(&mut conn).await?); If you're fetching multiple pub struct PatientStudy {
patient_id: String,
patient_name: Option<String>,
study_id: Option<String>
}
let patient_studies = sqlx::query_as!(
PatientStudy,
"SELECT patient_id, patient_name, study_id
FROM patients
INNER JOIN studies ON patients.id = studies.patient_id
..."
)
.fetch_all(&mut conn)
.await?; |
Beta Was this translation helpful? Give feedback.
-
Hello,
I'm trying to understand how to manage 1:N relationships, I've read all open issues on this subject but can't find a clear response.
For exemple :
Is there a way to retrieve a
Patient
and hisstudies
viaquery_as!
If yes how ?
If not, is there a way to tell SQLX to ignore the studies field in Patient (and then fill them manually via a second query_as!) ?
Beta Was this translation helpful? Give feedback.
All reactions