https://github.com/slick/slick-examples/blob/master/src/main/scala/scala/slick/examples/lifted/FirstExample.scalaのサンプルDBを使用してカウントクエリを作成しようとしました:
// 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)
H2で発生したエラーは次のとおりです。
列「X4.COF_NAME」が見つかりません。SQLステートメント: CREATE FORCE VIEW PUBLIC._1 AS 選択する X4.COF_NAME、 X4.SUP_ID、 X4.PRICE、 X4.SALES、 X4.TOTAL FROM SYSTEM_RANGE(1、1)[42122-168] org.h2.jdbc.JdbcSQLException:列"X4.COF_NAME"が見つかりません。SQLステートメント: CREATE FORCE VIEW PUBLIC._1 AS 選択する X4.COF_NAME、 X4.SUP_ID、 X4.PRICE、 X4.SALES、 X4.TOTAL FROM SYSTEM_RANGE(1、1)[42122-168] org.h2.message.DbException.getJdbcSQLException(DbException.java:329)で org.h2.message.DbException.get(DbException.java:169)で org.h2.message.DbException.get(DbException.java:146)で org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:138)で org.h2.command.dml.Select.prepare(Select.java:799)で org.h2.command.Parser.prepare(Parser.java:202)で org.h2.engine.Session.prepare(Session.java:388)で org.h2.engine.Session.prepare(Session.java:375)で org.h2.table.TableView.compileViewQuery(TableView.java:99)で org.h2.table.TableView.initColumnsAndTables(TableView.java:144)で org.h2.table.TableView.init(TableView.java:95)で org.h2.table.TableView。(TableView.java:61)で org.h2.table.TableView.createTempView(TableView.java:420)で org.h2.command.Parser.readTableFilter(Parser.java:1037)で org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1690)で org.h2.command.Parser.parseSelectSimple(Parser.java:1797)で org.h2.command.Parser.parseSelectSub(Parser.java:1684)で org.h2.command.Parser.parseSelectUnion(Parser.java:1527)で org.h2.command.Parser.readTableFilter(Parser.java:1026)で org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1690)で org.h2.command.Parser.parseSelectSimple(Parser.java:1797)で org.h2.command.Parser.parseSelectSub(Parser.java:1684)で org.h2.command.Parser.parseSelectUnion(Parser.java:1527)で org.h2.command.Parser.parseSelect(Parser.java:1515)で org.h2.command.Parser.parsePrepared(Parser.java:405)で org.h2.command.Parser.parse(Parser.java:279)で org.h2.command.Parser.parse(Parser.java:251)で org.h2.command.Parser.prepareCommand(Parser.java:217)で org.h2.engine.Session.prepareLocal(Session.java:415)で org.h2.engine.Session.prepareCommand(Session.java:364)で org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1109)で org.h2.jdbc.JdbcPreparedStatement。(JdbcPreparedStatement.java:74)で org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:626)で scala.slick.session.Session $ class.prepareStatement(Session.scala:29)で scala.slick.session.BaseSession.prepareStatement(Session.scala:201)で scala.slick.jdbc.StatementInvoker.results(StatementInvoker.scala:29)で scala.slick.jdbc.StatementInvoker.elementsTo(StatementInvoker.scala:17)で scala.slick.jdbc.Invoker $ class.foreach(Invoker.scala:90)で scala.slick.jdbc.StatementInvoker.foreach(StatementInvoker.scala:10)で scala.slick.jdbc.Invoker $ class.build(Invoker.scala:66)で scala.slick.jdbc.StatementInvoker.build(StatementInvoker.scala:10)で scala.slick.jdbc.Invoker $ class.list(Invoker.scala:56)で scala.slick.jdbc.StatementInvoker.list(StatementInvoker.scala:10)で scala.slick.jdbc.UnitInvoker $ class.list(Invoker.scala:150)で scala.slick.driver.BasicInvokerComponent $ QueryInvoker.list(BasicInvokerComponent.scala:19)で
間違った使い方によるものですか?バグですか?
使用するとエラーが発生します
println("query1 = " + query1.firstOption.getOrElse(0))
同じように...