How to use COUNT (*) in Slick 3.0?

I have been using Slick for quite some time, and now I am migrating from Slick 2.1 to 3.0. Unfortunately, I am stuck with ordinary things like counting lines. My code worked fine in Slick 2.1 when I did this:

connection.withSession { implicit session => coffees.length.run } 

In the above code, I get my result as Int, but I can't get it to work now after I switched to Slick 3.0.2 although the documentation tells me that the code should be the same .

I tried the following (I already deleted the deprecated call withSession):

 connection.createSession.withTransaction { coffees.length } 

But this code will return slick.lifted.Rep [Int], which has no way to get an integer value. Am I missing implicit imports?

+5
source share
2 answers

As you probably understood, the result of calling run is to create a Future that will be resolved at some later point.

While this means that ultimately somewhere in the code, the future will have to wait as you show in your answer, this can and should be delayed as late as possible. If you work with, for example, the Play platform, use async Actions and let Play process it for you.

Meanwhile, working with Future , like any other monadic construct (for example, Option ), is a call to map , flatMap , onSuccess , etc., to link your calculations inside the common Future context.

+3
source

Please tell me that there is a better way to answer my question. I got this to work, but it looks awful:

 import scala.concurrent.duration._ import scala.concurrent.Await val timeout = Duration(10, SECONDS) val count = Await.result(connection.run(coffees.length.result), timeout) 
+1
source

All Articles