在Slick 2.0中插入后返回AutoInc ID [英] Returning AutoInc ID after Insert in Slick 2.0

查看:88
本文介绍了在Slick 2.0中插入后返回AutoInc ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从天涯海角寻找了这个问题的答案.关于slick 2.0的信息不多.下面是我的Addresses模型的代码,在插入后,如何让create方法返回ID?

I have looked to the ends of the earth for the answer to this question. There is not much info out there on slick 2.0. Below is my code for my Addresses model, how would I have the method create return the id after it made the insert?

package models
import play.api.Play.current
import play.api.db.slick.Config.driver.simple._
import play.api.db.slick.DB

    object Addresses{
      val DB_URL:String = "jdbc:h2:mem:fls-play"
      val DB_driver:String = "org.h2.Driver"
      class Addresses(tag: Tag) extends Table[(String, String, String, String, String)](tag, "ADDRESSES"){
       def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
       def city = column[String]("CITY")
       def stateName = column[String]("STATE_NAME")
       def street1 = column[String]("STREET1")
       def street2 = column[String]("STREET2")
       def zip = column[String]("ZIP")

       def * = (city, stateName, street1, street2, zip)
      }
      val addresses = TableQuery[Addresses]

      def create(city:String, stateName:String, street1:String, street2:String, zip:String) {
        DB.withSession{ implicit session =>
           addresses += (city, stateName, street1, street2, zip)
        }
      }
    }

谢谢!

推荐答案

尝试

(addresses returning addresses.map(_.id)) += (city, stateName, street1, street2, zip)

尚未对其进行测试,但在此处

haven't tested it, but it is shown here

这篇关于在Slick 2.0中插入后返回AutoInc ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆