Trouble with creating tables in the examples -- am I doing something wrong?

153 views
Skip to first unread message

David Walend

unread,
Feb 27, 2015, 4:00:34 PM2/27/15
to scala...@googlegroups.com
In an effort to understand why table.ddl.create isn't working in my own slick code, I'm cutting and pasting from the example in http://slick.typesafe.com/doc/2.1.0/gettingstarted.html . The code is pretty straight-forward, but it's not creating tables.

Is it related to https://github.com/slick/slick/issues/155 ? Is there a sane work-around?

Thanks,

Dave

---

Specifically, pasting this in the REPL:

import scala.slick.driver.H2Driver.simple._
class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "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)
}
val suppliers
= TableQuery[Suppliers]
// Definition of the COFFEES table
class Coffees(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "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)
}
val coffees
= TableQuery[Coffees]
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
 
implicit session =>
(suppliers.ddl ++ coffees.ddl).create
}
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
 
implicit session =>
suppliers
+= (101, "Acme, Inc.",      "99 Market Street", "Groundsville", "CA", "95199")
}

Results in 

<console>:15: warning: Adapting argument list by creating a 6-tuple: this may not be what you want.
        signature: InsertInvokerDef.+=(value: U)(implicit session: scala.slick.jdbc.JdbcBackend#SessionDef): InsertInvokerDef.this.SingleInsertResult
  given arguments: 101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"
 after adaptation: InsertInvokerDef.+=((101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"): (Int, String, String, String, String, String))
              suppliers += (101, "Acme, Inc.",      "99 Market Street", "Groundsville", "CA", "95199")
                        ^
org.h2.jdbc.JdbcSQLException: Table "SUPPLIERS" not found; SQL statement:
insert into "SUPPLIERS" ("SUP_ID","SUP_NAME","STREET","CITY","STATE","ZIP")  values (?,?,?,?,?,?) [42102-185]
  at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
  at org.h2.message.DbException.get(DbException.java:179)
  at org.h2.message.DbException.get(DbException.java:155)
  at org.h2.command.Parser.readTableOrView(Parser.java:5239)
  at org.h2.command.Parser.readTableOrView(Parser.java:5216)
  at org.h2.command.Parser.parseInsert(Parser.java:1031)
  at org.h2.command.Parser.parsePrepared(Parser.java:401)
  at org.h2.command.Parser.parse(Parser.java:305)
  at org.h2.command.Parser.parse(Parser.java:277)
  at org.h2.command.Parser.prepareCommand(Parser.java:242)
  at org.h2.engine.Session.prepareLocal(Session.java:446)
  at org.h2.engine.Session.prepareCommand(Session.java:388)
  at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1189)
  at org.h2.jdbc.JdbcPreparedStatement.
<init>(JdbcPreparedStatement.java:72)
  at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:666)
  at scala.slick.jdbc.JdbcBackend$SessionDef$class.prepareStatement(JdbcBackend.scala:152)
  at scala.slick.jdbc.JdbcBackend$BaseSession.prepareStatement(JdbcBackend.scala:389)
  at scala.slick.jdbc.JdbcBackend$SessionDef$class.withPreparedStatement(JdbcBackend.scala:190)
  at scala.slick.jdbc.JdbcBackend$BaseSession.withPreparedStatement(JdbcBackend.scala:389)
  at scala.slick.driver.JdbcInsertInvokerComponent$BaseInsertInvoker.preparedInsert(JdbcInsertInvokerComponent.scala:170)
  at scala.slick.driver.JdbcInsertInvokerComponent$BaseInsertInvoker.internalInsert(JdbcInsertInvokerComponent.scala:180)
  at scala.slick.driver.JdbcInsertInvokerComponent$BaseInsertInvoker.insert(JdbcInsertInvokerComponent.scala:175)
  at scala.slick.driver.JdbcInsertInvokerComponent$InsertInvokerDef$class.$plus$eq(JdbcInsertInvokerComponent.scala:70)
  at scala.slick.driver.JdbcInsertInvokerComponent$BaseInsertInvoker.$plus$eq(JdbcInsertInvokerComponent.scala:145)
  at $anonfun$1.apply(
<console>:15)
  at $anonfun$1.apply(
<console>:14)
  at scala.slick.backend.DatabaseComponent$DatabaseDef$class.withSession(DatabaseComponent.scala:34)
  at scala.slick.jdbc.JdbcBackend$DatabaseFactoryDef$$anon$4.withSession(JdbcBackend.scala:61)
  ... 49 elided



David Walend

unread,
Feb 27, 2015, 4:21:02 PM2/27/15
to scala...@googlegroups.com
Looks like it has something to do with H2's in-memory db. Switching the real project to use a file system fixed it. (But it's for testing, so I'd rather have in-memory.)

Any idea what's out of joint?

Thanks,

Dave

David Walend

unread,
Feb 27, 2015, 4:47:35 PM2/27/15
to scala...@googlegroups.com
I've figured it out. By default H2 drops the database when the last connection goes away. 

  val database = Database.forURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver")

Sorry for interrupting your day. Hope this saves someone some pain at some point.

Dave

Stefan Zeiger

unread,
Mar 2, 2015, 6:19:12 AM3/2/15
to scala...@googlegroups.com
On 2015-02-27 22:47, David Walend wrote:
I've figured it out. By default H2 drops the database when the last connection goes away. 

  val database = Database.forURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver")

Sorry for interrupting your day. Hope this saves someone some pain at some point.

In Slick 3.0 you can configure your database with connectionPool=null and keepAliveConnection=true to keep the database instance alive for the lifetime of the Database object. See the "Hello Slick" tutorial for details.

--
Stefan Zeiger
Slick Tech Lead
Typesafe - Build Reactive Apps!
Twitter: @StefanZeiger

David Walend

unread,
Mar 2, 2015, 10:18:05 AM3/2/15
to scala...@googlegroups.com
On Monday, March 2, 2015 at 6:19:12 AM UTC-5, Stefan Zeiger wrote:

In Slick 3.0 you can configure your database with connectionPool=null and keepAliveConnection=true to keep the database instance alive for the lifetime of the Database object. See the "Hello Slick" tutorial for details.
 
That's what I want. Looking forward to it.

Thanks,

Dave
Reply all
Reply to author
Forward
0 new messages