Slick 3 - H2 auto increment and identity vs explicit primary key

277 views
Skip to first unread message

Aldo Stracquadanio

unread,
Jun 17, 2015, 6:21:03 AM6/17/15
to scala...@googlegroups.com
I am using Slick 3.0 with MySQL for production and H2 for unit/functional testing and I stumbled upon a problem: I have a table definition akin to:

  class Coffees(tag: Tag) extends Table[Coffee](tag, "coffee") {
    def id = column[Option[Int]]("id", O.AutoInc)
    def name = column[String]("name")
    def price = column[Price]("price")

    def * = (id, name, price, fulfilmentMethod, brandId, minOrderPrice, maxOrderPrice, freeDeliveryThreshold, minTimeBeforeTravel)
  }

Now I want to add an explicit primary key to this but if I try to do this:

def pk = primaryKey("coffee_pk", id)

I get errors when trying to create the schema on H2. The error is due to the fact that the id column will be defined by the following SQL:

"id" INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1)


With reference to https://code.google.com/p/h2database/issues/detail?id=363 I think that slick should use the AUTOINCREMENT bit instead of IDENTITY, so that I can define a primary key to be used when I create the schema on other databases (e.g. on MySQL).

Naftoli Gugenheim

unread,
Jul 6, 2015, 3:33:12 PM7/6/15
to scala...@googlegroups.com
Slick generates different SQL depending on what slick driver you're using. If you want to use the same table with different databases you need to abstract over which driver you use and fill in the right one at the right time.

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/58964e62-274a-461a-a226-a18e460636b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages