The following working code from Slick 2.1 returns a single integer (which in this example, happens to be the result of running a function called "foobar"):
def getFoobar(): Int = DB.withSession {
val query = Q.queryNA[Int]("select foobar()")
query.first
}
How would one port this to Slick 3.0? According to the Slick 3.0 docs, the query would have to be converted to an DBIOAction. So this is what I've tried:
import driver.api._
...
def getFoobar(): Future[Int] = {
val query = sql"select foobar()".as[Int]
db.run(query)
}
but this results in the following compilation error:
[error] found : slick.profile.SqlStreamingAction[Vector[Int],Int,slick.dbio.Effect]#ResultAction [Int,slick.dbio.NoStream,slick.dbio.Effect]
[error] required: MyDAO.this.driver.api.DBIO[Seq[Int]]
It appears that the sql interpolator is yielding a SqlStreamingAction
rather than a DBIO
, as db.run
is expecting.
What would be the correct way to write this in the new Slick 3 API?
Seq[Int]
, can you maybe try changing your as to.as[Seq[Int]].head
? According to the docs your query should work, so not sure what other issues there might be. – Akos Krivachysql"select foobar()".as[Int].head
to typecheck. This may be the correct way of doing it, but I don't want to close this answer until I find out that it behaves correctly at runtime. – kes