2

这是与其他自动增量问题类似的问题,但我不需要提供列列表来定义 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)
4

1 回答 1

0

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

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

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

于 2013-01-03T00:32:44.047 回答