Skip to content

Diesel allows to mix aggregate and unaggregate expressions via ORDER BY clauses #4448

@weiznich

Description

@weiznich

Setup

Versions

  • Rust: 1.83.0
  • Diesel: 2.2.6
  • Database: Postgresql 16.3
  • Operating System Fedora

Feature Flags

  • diesel: postgres

Problem Description

use diesel::prelude::*;

diesel::table! {
    users {
        id -> Integer,
        name -> Text,
    }
}

fn main() {
    let mut conn = PgConnection::establish("postgres://localhost/diesel_tests").unwrap();
    users::table
        .select(diesel::dsl::max(users::id))
        .order_by(users::name)
        .load::<Option<i32>>(&mut conn)
        .unwrap();
}

What are you trying to accomplish?

Build a query that uses ORDER BY and aggregate functions

What is the expected output?

The compilation fails as the query is invalid

What is the actual output?

Compilation succeeds, but the application fails to run the query with the following message:

thread 'main' panicked at src/main.rs:16:10:
called `Result::unwrap()` on an `Err` value: DatabaseError(Unknown, "column \"users.name\" must appear in the GROUP BY clause or be used in an aggregate function")

Steps to reproduce

Run the code above

Checklist

  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions