我試圖從https://github.com/slick/slick-examples/blob/master/src/main/scala/scala/slick/examples/lifted/FirstExample.scala建立與樣本數據庫計數查詢:斯卡拉油滑0.11.2計數與H2
// 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提出的是:
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)
是由於錯誤的用法?這是一個錯誤嗎?
如果我使用
println("query1 = " + query1.firstOption.getOrElse(0))
以及將引發錯誤...
發送到數據庫的語句包含在錯誤消息中。表'SYSTEM_RANGE(1,1)'是一個虛擬表,與Oracle表「DUAL」非常相似。它只知道列「X」(而不是「COF_NAME」等)。 –
@ThomasMueller我不明白JDBC級別的問題。我只是想用Slick框架(一個非常基本的情況)查詢一個表中的記錄總數,並且它失敗了,因爲它看起來框架沒有正確生成預期的SQL。 – Brice
是的。不幸的是,我不知道Slick,所以我唯一可以幫忙的方法就是解釋SQL異常的含義...... –