相关疑难解决方法(0)

Slick 3.0插入然后获得自动增量值

我写的这段代码完美无缺

class Items(tag: Tag) extends Table[Item](tag, "ITEMS") {
  def id = column[Long]("ITEMS_ID", O.PrimaryKey, O.AutoInc)
  def name = column[String]("ITEMS_NAME")
  def price = column[Double]("ITEMS_PRICE")
  def * = (id, name, price) <> ((Item.apply _).tupled, Item.unapply _)
}

object Shop extends Shop{
  val items = TableQuery[Items]
  val db = Database.forConfig("h2mem1")

  def create(name: String, price: Double) : Int = {
    val action = items ++= Seq(Item(0, name, price))
    val future1 = db.run(action)
    val future2 = future1 map {result => 
      result map {x => x}
    } …
Run Code Online (Sandbox Code Playgroud)

scala slick slick-3.0

31
推荐指数
2
解决办法
2万
查看次数

Scala&Play!&Slick&PostgreSQL自动增量

我在Scala中有以下代码:

case class Product(id: Option[Long] = None, name: String, price: BigDecimal, description: String)

object Products extends Table[Product]("product") {
  def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
  def name = column[String]("name", O.NotNull)
  def price = column[BigDecimal]("price", O.NotNull)
  def description = column[String]("description", O.NotNull)

  def * = id.? ~ name ~ price ~ description <>(Product.apply _, Product.unapply _)

  def autoInc = * returning id

  def add(product: Product)(implicit s:Session): Long = {
    Products.autoInc.insert(product)
  }

  def all(implicit s:Session): List[Product] = {
    Query(Products).list
  }
}
Run Code Online (Sandbox Code Playgroud)

列出所有产品效果很好,但是,我无法使添加方法有效.

致电后:

val …
Run Code Online (Sandbox Code Playgroud)

postgresql scala playframework slick playframework-2.1

5
推荐指数
1
解决办法
2965
查看次数