如何在这个光滑的通用DAO特征中用类型参数替换id类型?

时间:2016-01-26 07:47:12

标签: scala slick

如何用类型参数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将是一个 原始类型?

1 个答案:

答案 0 :(得分:1)

override def store(t: T)
(implicit shape: Shape[_ <: FlatShapeLevel, Rep[ID], ID, Rep[ID]])

您可能需要提供这样的形状

相关问题