Skip to content

One to one mapping #3152

Answered by weiznich
pintariching asked this question in Q&A
Apr 28, 2022 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

Yes that's possible using a join + a custom select clause. Assuming both structs implement Queryable the following code should work:

// 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)?;

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@pintariching
Comment options

@weiznich
Comment options

@pintariching
Comment options

Answer selected by pintariching
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants