Slick 3.0 Insert and then get Auto Increment Value

前端 未结 2 415
陌清茗
陌清茗 2020-12-13 04:21

I have written this code which works perfectly

class Items(tag: Tag) extends Table[Item](tag, \"ITEMS\") {
  def id = column[Long](\"ITEMS_ID\", O.PrimaryKey         


        
2条回答
  •  半阙折子戏
    2020-12-13 04:35

    Here's the relevant documentation page, according to which, you should construct a query like this:

    val insertQuery = items returning items.map(_.id) into ((item, id) => item.copy(id = id))
    
    def create(name: String, price: Double) : Future[Item] = {
      val action = insertQuery += Item(0, name, price)   
      db.run(action)
    }
    

提交回复
热议问题