Skip to content

Select only part of fields #304

Answered by billy1624
YoshieraHuang asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @YoshieraHuang, yes you can!

// Selecting the name column only
assert_eq!(
    cake::Entity::find()
        .select_only()
        .column(cake::Column::Name)
        .build(DbBackend::Postgres)
        .to_string(),
    r#"SELECT "cake"."name" FROM "cake""#
);

And, you can derive FromQueryResult if want to select partial fields into custom struct.

#[derive(Debug, FromQueryResult, PartialEq)]
struct BakerLite {
name: String,
}
#[derive(Debug, FromQueryResult, PartialEq)]
struct CustomerLite {
name: String,
}
let baked_for_customers: Vec<(BakerLite, Option<Customer…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by tyt2y3
Comment options

You must be logged in to vote
0 replies
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
Converted from issue

This discussion was converted from issue #302 on November 08, 2021 05:35.