Search code examples
scalaanorm

Scala Anorm select 2 values using column resolution


According to documentation (https://www.playframework.com/documentation/2.5.x/Anorm), I can do the following to retrieve values from 2 columns:

val res: (String, Int) = SQL"SELECT text, count AS i".map(row =>
  row[String]("text") -> row[Int]("i")
)

This does not compile...

Causes this:

Expression of type SimpleSql[(String, Int)] doesn't conform to expected type (String, Int)

I'm just looking for a single method of doing this (for anorm 2.5+). I was using regular parsers but am looking for this more concise way of doing it.


Solution

  • The code is not complete: to get a single result as such tuple, the .single combinator must be used.

    val res: (String, Int) = SQL"SELECT text, count AS i".map(row =>
      row[String]("text") -> row[Int]("i")
    ).single
    

    Using Anorm flatteners is easier for tuple result: see examples