Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to COUNT(*) in Slick 2.0?

According to the Slick 2.0 documentation, to get the count of rows in a table:

val q1 = coffees.length
// compiles to SQL (simplified):
//   select count(1) from "COFFEES"

However, it turns out that coffees.length is of type Column[Int].

How does one execute the query and get the value?

like image 541
kes Avatar asked Feb 02 '14 20:02

kes


3 Answers

I just had this same problem upgrading to slick 2.0. I forget where the exact method lives, but the generic .run seems to work for me, i.e.

coffees.length.run
like image 137
waffle paradox Avatar answered Oct 16 '22 02:10

waffle paradox


StaticQuery.queryNA[Int]("select count(*) from \"" + TableName + "\"").first

Quotes are needed if your table name is not upper case.

like image 5
tuxSlayer Avatar answered Oct 16 '22 02:10

tuxSlayer


Try coffees.length.first should execute and return Int

Sorry, indeed, in the slick 1.0 there was first method to do this, in Slick 2.0 they get rid of it in favor of more generic run.

The function to execute query is

coffees.length.run
like image 1
vitalii Avatar answered Oct 16 '22 02:10

vitalii