假设我想在我将与 slick 一起使用的所有案例类中都有一个 ID 列:
abstract class BaseEntity(val _id:Option[Long])
case class SomeEntity(id:Option[Long],value:String) extends BaseEntity(id)
现在让我们定义方案的抽象类和一个真正的方案:
abstract class BaseScheme[A <: BaseEntity](tag:Tag,name:String) extends Table[A](tag,name) {
def id = column[Long]("ID",O.PrimaryKey,O.AutoInc)
}
class SomeEntityScheme(tag:Tag) extends BaseScheme[SomeEntity](tag,"SOME_NAME") {
def value = column[String]("value",O.NotNull)
def * = (id.?,value) <> (SomeEntity.tupled,SomeEntity.unapply)
}
val someEntitiesTable = TableQuery[SomeEntityScheme]
现在,因为我认为我对编写 AutoIncInsert 方法感到厌烦,所以我将创建一个通用的方法,这就是我失败的时刻:
object BaseScheme {
def autoIncInsert[T <: BaseScheme[_]] (t : TableQuery[T]) = t returning t.map(_.id) into {case (p,id) => p.copy(id = Some(id))}
}
当然还有问题的输出:
[error] /home/tomaszk/Projects/4.4/mCloud/application/service-execution/execution-provisioning-model/src/main/scala/pl/mlife/mcloud/provisioning/database/schemes/ExcludingServicesScheme.scala:48: could not find implicit value for evidence parameter of type scala.slick.ast.TypedType[T#TableElementType]
[error] def remap[T <: BaseScheme[_]] (t : TableQuery[T]) = t returning t.map(_.id) into {case (p,id) => p.copy(id = Some(id))}
[error] ^
[error] one error found