Scala Slick自动增量场

时间:2013-01-03 00:24:31

标签: mysql scala slick

这是与其他自动增量问题类似的问题,但我没有提供用于定义autoInc的列列表,而是需要以某种方式提供表引用。我正在使用MySQL

import scala.slick.driver.MySQLDriver.simple._
import play.Logger
import slick.session.Database.threadLocalSession

object PaypalCartItems extends Table[PaypalCartItem]("paypalCartItem") {
  def id          = column[Int]("id", O.PrimaryKey, O.AutoInc)
  def itemName    = column[String]("item_name")
  def sku         = column[String]("item_number")
  def quantity    = column[String]("quantity")
  def txnId       = column[String]("txn_id")
  def url         = column[String]("url")

  def * = itemName ~ sku ~ quantity ~ txnId ~ url ~ id <> (PaypalCartItem, PaypalCartItem.unapply _)

  // I don't want to list out the columns, I need to reference the table
  def autoInc = itemName ~ sku ~ quantity ~ txnId ~ url returning id

  def insertItems(items: List[PaypalCartItem]): List[PaypalCartItem] = items map { item: PaypalCartItem =>
    val newId = PaypalCartItems.autoInc.insert(item)
    item.copy(id=newId)
  }
}

以上代码无法编译,提供以下内容:

[error]   overloaded method value insert with alternatives:
[error]   [TT](query: scala.slick.lifted.Query[TT,(String, String, String, String, String)])(implicit session: scala.slick.session.Session)Seq[Int] <and>
[error]   (value: (String, String, String, String, String))(implicit session: scala.slick.session.Session)Int
[error]  cannot be applied to (PaypalCartItem)
[error]     val newId = PaypalCartItems.autoInc.insert(item)

1 个答案:

答案 0 :(得分:0)

不确定它会对你有所帮助,但我会这样做:

def autoInc2 = * returning id.? into {
      case (Music(_, title, artist, album, isrc, ownerId), id) => Music(id, title, artist, album, isrc, ownerId)
    }

音乐的第一个参数是id,在这种情况下是Option [Int]。它用作val musicWithId = Musics.autoInc2.insert(someMusicObjectWithId)