Slick 3.0 Insert and then get Auto Increment Value

前端 未结 2 413
陌清茗
陌清茗 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)
    }
    
    0 讨论(0)
  • 2020-12-13 04:43

    Try this one instead:

    def create(name: String, price: Double): Future[Int] = db.run {
        (items returning items.map(_.id)) += Item(0, name, price)
    }
    
    0 讨论(0)
提交回复
热议问题