How to get the auto increment id just inserted by ScalaQuery

I am using ScalaQuery to access PostgreSql. Datais a table with an auto-incrementing key key with a name idthat is defined as def id = column[Long] ("id", O NotNull, O PrimaryKey, O AutoInc, O DBType "serial"). I use Data.insert(name, filename)to create a new record Data. Is there a way to get a idnewly created record? The method insertreturns only the int value returned executeUpdate.

+3
source share
1 answer

ScalaQuery does not support the sequence identifier out of the box, you need to define a helper method first:

val seqID = SimpleFunction.nullary[Int]("LASTVAL")

then use the helper helper to do your insert:

db withSession {
  val q  = Foo.insert(someInstance)
  val id = Query(seqID) // call sequence helper
}

re: , , :
fooobar.com/questions/31040/...

:
https://github.com/szeiger/scala-query/issues/10#issuecomment-698418

+3

All Articles