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