Search code examples
scalajdbcscalaquery

Get auto increment key in scalaquery


I want to insert rows in 2 tables which represent a 1:n relationship using scalaquery / slick.

The tables are defined as follows:

object CompanyBaseTable extends Table[CompanyBaseTableEntry]("company") {
  def id = column[Int]("id", O PrimaryKey, O AutoInc)
}

object ProductCatalogueTable extends Table[ProductCatalogueEntry]("product_catalogue") {
 def cid = column[Int]("id", O NotNull)
 def pid = column[Long]("pid", O NotNull)
 def company = foreignKey("company_fk", cid, CompanyBaseTable)(_.id)
}

I want to insert the 1:n relation within one transaction but I don't know how I can achieve that using ScalaQuery. In JDBC 3 you can get the generated ID from the statement, but I don't see where the statement is exposed in the ScalaQuery API, neither do I see a way to access this information directly.


Solution

  • Scala-query is now SLICK. There was an issue raised on github but it seems to have been fixed. This is the commit if you want to have a look at it.

    This answer also tells you how to get the id https://stackoverflow.com/a/13114949/152601