Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
weiznich opened this issue Jan 23, 2025 · 0 comments
Open
3 tasks done

Comments

@weiznich
Copy link
Member

weiznich commented Jan 23, 2025

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant