How to get scalar values from aggregate queries in 3.0

213 views
Skip to first unread message

Will Skinner

unread,
May 4, 2015, 2:48:07 AM5/4/15
to scala...@googlegroups.com
I'm new to Slick and have have been unable to answer this seemingly simple question after reading through the 3.0.0 docs. I'm interested in counting the number of rows in a table, which in SQL would look like:
select count(*) from table;


In Slick 2 I can do something like this:
val countQuery: Column[Int] = coffees.length
val count: Int = countQuery.run

The Slick 3 docs give an example:
val q1 = coffees.length

in this case q1 has type
 Rep[Int]
and after reading through http://slick.typesafe.com/doc/3.0.0/queries.html and various other sources, I still can't figure out how to get an
Int
value from this.

Anze Cesar

unread,
May 4, 2015, 12:12:00 PM5/4/15
to scala...@googlegroups.com
Hi,

you get a (future of) result by executing the action, like so:

val q = coffees.length
val action = q.result
val result: Future[Int] = db.run(action)

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/b6d1c203-b8f8-4490-829d-3ad0b2bfe1a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Will Skinner

unread,
May 4, 2015, 12:19:04 PM5/4/15
to scala...@googlegroups.com
Trying to do 
q.result
gives - 'cannot resolve symbol result'. Perhaps there is an implicit conversion going on from
Rep[Int]
and I need to import something?

Anze Cesar

unread,
May 4, 2015, 12:24:39 PM5/4/15
to scala...@googlegroups.com
Yes,
import slick.driver.PostgresDriver.api._

replace with correct driver. I think that should do it. If you're using IntelliJ Idea, it won't resolve 'result', but it compiles and works just fine.

Will Skinner

unread,
May 4, 2015, 12:27:15 PM5/4/15
to scala...@googlegroups.com
Thanks! This solved the problem.
Reply all
Reply to author
Forward
0 new messages