As described in the Slick documentation , you can use the returning <Table>.id
.
Suppose you have a cat defined in a slippery manner:
case class Cat(id: Option[Int], name: String) object Cats extends Table[Cat]("cats") { def id = column[Int]("cat_id", O.PrimaryKey, O.AutoInc) def name = column[String]("name") def * = id.? ~ name <> (Cat.apply _, Cat.unapply _) }
Then you can get the newly created id
the moment you insert a new cat.
val newCatId = Cats returning Cats.id insert Cat(None, "Cringer")
Hope that helps;)
source share