question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Grouping by Option[List[String]] fails with must appear in the GROUP BY clause or be used in an aggregate function

See original GitHub issue

Hello,

I’m having an issue trying to group by a nullable array column with a default.

I have a table like this

trait TestTableSpec { this: TestSchema with PgArraySupport =>

  import api._

  class TestTable(tag: Tag) extends Table[Test](tag, "test") {

    def id = column[Int]("id")
    def timestamp = column[LocalDateTime]("timerino")
    def one = column[Int]("one")
    def optionInt = column[Option[Int]]("two")
    def three = column[List[Int]]("three")
    def four = column[Option[List[String]]]("four")

    def * = (id, timestamp, one, optionInt, three, four) <> ((Test.apply _).tupled, Test.unapply)

  }

  val tests = lifted.TableQuery[TestTable]

}

case class Test(id: Int, time: LocalDateTime, one: Int, optionInt: Option[Int], three: List[Int], four: Option[List[String]])

And I’m trying to perform a query as such:

  def caseTest = {
    db.run(
        tests.groupBy(table =>
          table.four.getOrElse(List("No"))

//          The below works
//          table.four

// Tried the below as well, but results in "Caught exception while computing default value for Rep[Option[_]].getOrElse -- This cannot be done lazily when the value is needed on the database side"
//          (Case If table.four.isDefined Then table.four.get Else List("No")).unnest
        ).map(q => q._1 -> q._2.size).result

    )
  }

However, the query results in the following:

[info] 1418 [scala-execution-context-global-15] DEBUG slick.basic.BasicBackend.action - #1: result [select coalesce("four", ?), count(1) from "test" group by coalesce("four", ?)]
[info] 1419 [test-2] DEBUG slick.jdbc.JdbcBackend.statement - Preparing statement: select coalesce("four", ?), count(1) from "test" group by coalesce("four", ?)
[info] 1425 [test-2] DEBUG slick.jdbc.JdbcBackend.statement - Executing prepared statement: HikariProxyPreparedStatement@809193186 wrapping select coalesce("four", '{"No"}'), count(1) from "test" group by coalesce("four", '{"No"}')
[info] 1425 [test-2] DEBUG s.j.J.statementAndParameter - Executing prepared statement: HikariProxyPreparedStatement@809193186 wrapping select coalesce("four", '{"No"}'), count(1) from "test" group by coalesce("four", '{"No"}')
[info] 1430 [test-2] DEBUG slick.jdbc.JdbcBackend.parameter - /--------+--------\
[info] 1430 [test-2] DEBUG slick.jdbc.JdbcBackend.parameter - | 1      | 2      |
[info] 1430 [test-2] DEBUG slick.jdbc.JdbcBackend.parameter - | Array  | Array  |
[info] 1430 [test-2] DEBUG slick.jdbc.JdbcBackend.parameter - |--------+--------|
[info] 1430 [test-2] DEBUG slick.jdbc.JdbcBackend.parameter - | {"No"} | {"No"} |
[info] 1430 [test-2] DEBUG slick.jdbc.JdbcBackend.parameter - \--------+--------/
[error] org.postgresql.util.PSQLException: ERROR: column "test.four" must appear in the GROUP BY clause or be used in an aggregate function

Copying the exact query from the logs and running it in postgres console works just fine, so I’m a little stumped.

Thanks for your time.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
tmingleicommented, May 20, 2019

Released in v0.17.3

0reactions
tmingleicommented, May 2, 2019

Can be soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

must appear in the GROUP BY clause or be used in an ...
Yes, this is a common aggregation problem. Before SQL3 (1999), the selected fields must appear in the GROUP BY clause[*]. To workaround this...
Read more >
column must appear in the GROUP BY clause or be used in ...
Above works on PostgreSQL 10 but fails on older versions. Older versions tell me this error message: ERROR: column "mytable.col1" must appear in ......
Read more >
Column < name > must appear in the GROUP BY clause or be ...
This page can help you troubleshoot the column name must appear in the GROUP BY clause or be used in an aggregate function...
Read more >
2 Rules To Follow When Using GROUP BY in SQL
Fix must appear in the GROUP BY clause or be used in an aggregate function. How to fix ERROR: aggregate functions are not...
Read more >
U118: Column must appear in the GROUP BY clause
ERROR : column "identifier_column.value_column" must appear in the GROUP BY clause or be used in an aggregate function at character 8 STATEMENT: SELECT ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found