Have you tried the following?
val query1 = for (c <- Coffees) yield c.name.count
println("query1 = " + query1.firstOption.getOrElse(0))
The recipe behind it is c.primary-key.count
; in this case, name
is the primary key of Coffees
.
Question
I tried to build a count query with an sample DB from https://github.com/slick/slick-examples/blob/master/src/main/scala/scala/slick/examples/lifted/FirstExample.scala :
// Definition of the SUPPLIERS table
object Suppliers extends Table[(Int, String, String, String, String, String)]("SUPPLIERS") {
def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column
def name = column[String]("SUP_NAME")
def street = column[String]("STREET")
def city = column[String]("CITY")
def state = column[String]("STATE")
def zip = column[String]("ZIP")
// Every table needs a * projection with the same type as the table's type parameter
def * = id ~ name ~ street ~ city ~ state ~ zip
}
// Definition of the COFFEES table
object Coffees extends Table[(String, Int, Double, Int, Int)]("COFFEES") {
def name = column[String]("COF_NAME", O.PrimaryKey)
def supID = column[Int]("SUP_ID")
def price = column[Double]("PRICE")
def sales = column[Int]("SALES")
def total = column[Int]("TOTAL")
def * = name ~ supID ~ price ~ sales ~ total
// A reified foreign key relation that can be navigated to create a join
def supplier = foreignKey("SUP_FK", supID, Suppliers)(_.id)
}
// Connect to the database and execute the following block within a session
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
(Suppliers.ddl ++ Coffees.ddl).create
// Insert some suppliers
Suppliers.insert(101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199")
Suppliers.insert( 49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460")
Suppliers.insert(150, "The High Ground", "100 Coffee Lane", "Meadows", "CA", "93966")
// Insert some coffees (using JDBC's batch insert feature, if supported by the DB)
Coffees.insertAll(
("Colombian", 101, 7.99, 0, 0),
("French_Roast", 49, 8.99, 0, 0),
("Espresso", 150, 9.99, 0, 0),
("Colombian_Decaf", 101, 8.99, 0, 0),
("French_Roast_Decaf", 49, 9.99, 0, 0)
)
/**** This part works fine */
val query = for (c <- Coffees) yield c
query foreach println
/*********/
/******This one fails */
val query1 = for (c <- Coffees) yield c.length
println("query1 = " + query1.list.head)
The error H2 raised was:
Column "X4.COF_NAME" not found; SQL statement: CREATE FORCE VIEW PUBLIC._1 AS SELECT X4.COF_NAME, X4.SUP_ID, X4.PRICE, X4.SALES, X4.TOTAL FROM SYSTEM_RANGE(1, 1) [42122-168] org.h2.jdbc.JdbcSQLException: Column "X4.COF_NAME" not found; SQL statement: CREATE FORCE VIEW PUBLIC._1 AS SELECT X4.COF_NAME, X4.SUP_ID, X4.PRICE, X4.SALES, X4.TOTAL FROM SYSTEM_RANGE(1, 1) [42122-168] at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) at org.h2.message.DbException.get(DbException.java:169) at org.h2.message.DbException.get(DbException.java:146) at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:138) at org.h2.command.dml.Select.prepare(Select.java:799) at org.h2.command.Parser.prepare(Parser.java:202) at org.h2.engine.Session.prepare(Session.java:388) at org.h2.engine.Session.prepare(Session.java:375) at org.h2.table.TableView.compileViewQuery(TableView.java:99) at org.h2.table.TableView.initColumnsAndTables(TableView.java:144) at org.h2.table.TableView.init(TableView.java:95) at org.h2.table.TableView.(TableView.java:61) at org.h2.table.TableView.createTempView(TableView.java:420) at org.h2.command.Parser.readTableFilter(Parser.java:1037) at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1690) at org.h2.command.Parser.parseSelectSimple(Parser.java:1797) at org.h2.command.Parser.parseSelectSub(Parser.java:1684) at org.h2.command.Parser.parseSelectUnion(Parser.java:1527) at org.h2.command.Parser.readTableFilter(Parser.java:1026) at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1690) at org.h2.command.Parser.parseSelectSimple(Parser.java:1797) at org.h2.command.Parser.parseSelectSub(Parser.java:1684) at org.h2.command.Parser.parseSelectUnion(Parser.java:1527) at org.h2.command.Parser.parseSelect(Parser.java:1515) at org.h2.command.Parser.parsePrepared(Parser.java:405) at org.h2.command.Parser.parse(Parser.java:279) at org.h2.command.Parser.parse(Parser.java:251) at org.h2.command.Parser.prepareCommand(Parser.java:217) at org.h2.engine.Session.prepareLocal(Session.java:415) at org.h2.engine.Session.prepareCommand(Session.java:364) at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1109) at org.h2.jdbc.JdbcPreparedStatement.(JdbcPreparedStatement.java:74) at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:626) at scala.slick.session.Session$class.prepareStatement(Session.scala:29) at scala.slick.session.BaseSession.prepareStatement(Session.scala:201) at scala.slick.jdbc.StatementInvoker.results(StatementInvoker.scala:29) at scala.slick.jdbc.StatementInvoker.elementsTo(StatementInvoker.scala:17) at scala.slick.jdbc.Invoker$class.foreach(Invoker.scala:90) at scala.slick.jdbc.StatementInvoker.foreach(StatementInvoker.scala:10) at scala.slick.jdbc.Invoker$class.build(Invoker.scala:66) at scala.slick.jdbc.StatementInvoker.build(StatementInvoker.scala:10) at scala.slick.jdbc.Invoker$class.list(Invoker.scala:56) at scala.slick.jdbc.StatementInvoker.list(StatementInvoker.scala:10) at scala.slick.jdbc.UnitInvoker$class.list(Invoker.scala:150) at scala.slick.driver.BasicInvokerComponent$QueryInvoker.list(BasicInvokerComponent.scala:19)
Is due to a wrong usage? Is it a bug?
The error is raised if I use
println("query1 = " + query1.firstOption.getOrElse(0))
as well...
Solution
Have you tried the following?
val query1 = for (c <- Coffees) yield c.name.count
println("query1 = " + query1.firstOption.getOrElse(0))
The recipe behind it is c.primary-key.count
; in this case, name
is the primary key of Coffees
.