lcappa lcappa - 1 year ago 242
Scala Question

Scala Slick: groupBy with nullable column

I am trying to get all results from a nullable column using Scala 2.11 + Slick 3.1.0 + Evolutions + H2 database. When executing groupBy operations over NOT NULL columns everything works as expected. However, if the column is nullable, when we run this this:

def listAllTenants(): Future[Seq[String]] = { => metric.tenant)
.map {
case (tenant, group) => tenant


create table `METRIC` (


`tenant` VARCHAR(255),

A exception is thrown:

[error] c.w.g.c.ErrorHandler - Error while processing request. Exception> slick.SlickException: Read NULL value (null) for ResultSet column <computed>

I have searched and tried adding an extra case trying to cover empty or null values, but it didn't work, or perhaps my case was incorrect.

Thanks in advance.

Answer Source

The definition of the column should be nullable, ie: def tenant = column[Option[String]]def tenant = column[Option[String]].

Indeed, when doing metrics.groupBy(_.tenant).map { case (tenant, group) => tenant } the result Seq may contain a null value which cause the NPE you mention.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download