如何用类型参数Long
替换具体类型ID
?
trait GenericDaoProfile[T <: HasId[Long]] {
this: JdbcProfile =>
import driver.api._
trait HasIdColumn {
def id: Rep[Long]
}
trait GenericDao[TB <: Table[T] with HasIdColumn] extends Dao[Long, T] {
val query: TableQuery[TB]
override def store(t: T): Option[Long] = {
assert(t.id.isEmpty)
Some(Await.result(DB.db.run((query returning query.map(_.id)) += t), Duration.Inf))
}
}
}
我试过了:
trait GenericDaoProfile[ID, T <: HasId[ID]] {
this: JdbcProfile =>
import driver.api._
trait HasIdColumn {
def id: Rep[ID]
}
trait GenericDao[TB <: Table[T] with HasIdColumn] extends Dao[ID, T] {
val query: TableQuery[TB]
override def store(t: T): Option[ID] = {
assert(t.id.isEmpty)
// error for code below:
// Error:(42, 61) No matching Shape found.
// Slick does not know how to map the given types.
Some(Await.result(DB.db.run((query returning query.map(_.id)) += t), Duration.Inf))
}
}
}
似乎光滑不知道如何映射任意类型ID
但是原语。有没有办法告诉scala编译器ID
将是一个
原始类型?
答案 0 :(得分:1)
override def store(t: T)
(implicit shape: Shape[_ <: FlatShapeLevel, Rep[ID], ID, Rep[ID]])
您可能需要提供这样的形状