Skip to content

Using aggreagation functions (e.g. ARRAY_AGG) #1986

Closed Answered by cosmincatalin
cosmincatalin asked this question in Q&A
Discussion options

You must be logged in to vote

After some digging up, I have found out at least one way to solve this

Using custom expressions and custom struct

References:

This can probably be made prettier, but the concept stands

#[derive(FromQueryResult)]
struct ImplodedParentChildren {
    first_name: String,
    last_name: String,
    children: Vec<String>,
}

Parents::find()
    .select_only()
    .column_as(parents::Column::LastName, "first_name")
    .column_as(parents::Column::FirstName, "last_name")
    .column_as(Expr::cust("ARRAY_AGG(\"parents\".\"Child\")"), "children")
    .filter(
        Condition::all().add(
            Expr::tuple([
                Expr::col(parents::Column::LastName).

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ns-sjorgedeaguiar
Comment options

Answer selected by cosmincatalin
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