dynamically group by columns

90 views
Skip to first unread message

wer vetra

unread,
Apr 10, 2018, 4:45:51 PM4/10/18
to Slick / ScalaQuery
Similar to this issue, Is it possible to optionally groupby a column of known type (e.g. Option[String])? Is it possible to group by Seq of column name Strings (Seq[String])? And, is it possible to do both (Option[Seq[String]])?

Borrowing from this proposed solution, I can dynamically define the column in the group by, like:


def getSomethingGroupedBy(groupBy: String): Future[Seq[String]] = {
 val query
= someTable
 
.groupBy(cols => cols.column[String](groupBy)))
 
.map{
 
case (group, row) => group
 
}

 db
.run(query.result.transactionally)
}


But if I try to use a groupBy: Seq[String], I get:

Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection,
[error]  you use an unsupported type in a Query (e.g. scala List),
[error]  or you forgot to import a driver api into scope.
[error]   Required level: slick.lifted.FlatShapeLevel
[error]      Source type: Seq[slick.lifted.Rep[String]]
[error]    Unpacked type: T
[error]      Packed type: G
[error]       .groupBy(cols => groupBy.map(g => cols.column[String](g)))
[error]               ^
[error] one error found





How can I group by an optional and/or arbitrary Seq of string columns that are user defined?
Reply all
Reply to author
Forward
0 new messages