One to one mapping #3152
Answered
by
weiznich
pintariching
asked this question in
Q&A
One to one mapping
#3152
-
Let's say I have two structs like so:
Is it possible to write a simple single query to map these structs so I can use
|
Beta Was this translation helpful? Give feedback.
Answered by
weiznich
Apr 29, 2022
Replies: 1 comment 3 replies
-
Yes that's possible using a join + a custom select clause. Assuming both structs implement // For reference the corresponding table definitions
table! {
machines (id) {
id -> Integer,
name -> Text,
manufacturer_id -> Integer,
}
}
table! {
manufacturers (id) {
id -> Integer,
name -> Text,
}
}
joinable!(machines -> manufacturers (manufacturer_id));
// use the following query to receive the data in the correct shape:
machines::table.inner_join(manufacturers::table).select((machines::id, machines::name, machines::manufacturer_id, manufacturers::all_columns).load::<Machine>(&conn)?; |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
pintariching
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes that's possible using a join + a custom select clause. Assuming both structs implement
Queryable
the following code should work: