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

Trait `diesel::expression::NonAggregate` is not implemented for `diesel::dsl::CountStar` #1781

Closed
ondj opened this Issue Jul 7, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@ondj

ondj commented Jul 7, 2018

Setup

Versions

  • Rust: 1.27.0
  • Diesel: 1.3.2
  • Database: PostgreSQL
  • Operating System: Windows 10

Feature Flags

diesel = { version = "1.3.0", features = ["postgres", "chrono", "r2d2"] }

Problem Description

I am trying to get results from db grouped by column serial_to and get serial_to with number of entries for that serial.

What are you trying to accomplish?

Send to database SQL query like this:

SELECT COUNT(*), serial_to FROM inet GROUP BY serial_to

What is the expected output?

Vec<(i32, i64)>

What is the actual output?

Compilation error:

error[E0277]: the trait bound `diesel::dsl::CountStar: diesel::expression::NonAggregate` is not satisfied
  --> src/stats.rs:25:10
   |
25 |         .select((inet::serial_to, count_star()))
   |          ^^^^^^ the trait `diesel::expression::NonAggregate` is not implemented for `diesel::dsl::CountStar`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `(database_schema::inet::columns::serial_to, diesel::dsl::CountStar)`
   = note: required because of the requirements on the impl of `diesel::query_dsl::select_dsl::SelectDsl<(database_schema::inet::columns::serial_to, diesel::dsl::CountStar)>` for `database_schema::inet::table`

error[E0599]: no method named `group_by` found for type `diesel::query_builder::SelectStatement<database_schema::inet::table, diesel::query_builder::select_clause::SelectClause<(database_schema::inet::columns::serial_to, diesel::dsl::CountStar)>>` in the current scope
  --> src/stats.rs:26:10
   |
26 |         .group_by(inet::serial_to)
   |          ^^^^^^^^ private field, not a method
   |
   = note: the method `group_by` exists but the following trait bounds were not satisfied:
           `&diesel::query_builder::SelectStatement<database_schema::inet::table, diesel::query_builder::select_clause::SelectClause<(database_schema::inet::columns::serial_to, diesel::dsl::CountStar)>> : diesel::GroupByDsl<_>`
           `&mut diesel::query_builder::SelectStatement<database_schema::inet::table, diesel::query_builder::select_clause::SelectClause<(database_schema::inet::columns::serial_to, diesel::dsl::CountStar)>> : diesel::GroupByDsl<_>`

Are you seeing any additional errors?

No

Steps to reproduce

Database schema can be simple:

table! {
    inet (hash) {
        hash -> Binary,
        serial_to -> Int4,
    }
}

This is compiled correctly and works:

let stats: Vec<i32> = inet::table
        .select(inet::serial_to)
        .group_by(inet::serial_to)
        .load(&connection)?;

But this shows complication errors:

let stats: Vec<(i32, i64)> = inet::table
        .select((inet::serial_to, count_star()))
        .group_by(inet::serial_to)
        .load(&connection)?;

Checklist

  • I have already looked over the issue tracker for similar issues.
  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
@vmalloc

This comment has been minimized.

Contributor

vmalloc commented Jul 30, 2018

Also hitting this issue and was wondering if there is a workaround to getting it work...

@vmalloc

This comment has been minimized.

Contributor

vmalloc commented Jul 30, 2018

I managed to work around it by replacing count_star() with diesel::dsl::sql<diesel::sql_types::BigInt>("count(*)"). Yuck, but works...

@sgrif

This comment has been minimized.

Member

sgrif commented Jul 30, 2018

Diesel doesn't currently support .group_by, and for that reason we don't allow mixing aggregate/non-aggregate expressions. You can use the workaround above until we introduce support for group by

@sgrif sgrif closed this Jul 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment