value delete不是slick.lifted.Query的成员[T,T#TableElementType,Seq]

时间:2016-12-06 05:00:58

标签: scala playframework slick play-slick

我目前正在玩Play和Play-slick。以下代码给出了错误

class GenericRepository(protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile] {
  import driver.api._

  implicit val localDateTimeColumnType = MappedColumnType.base[LocalDateTime, Timestamp](
    d => Timestamp.from(d.toInstant(ZoneOffset.ofHours(0))),
    d => d.toLocalDateTime
  )

  protected trait GenericTable {
    this: Table[_] =>
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
    def createdAt = column[LocalDateTime]("created_at")
    def updatedAt = column[LocalDateTime]("updated_at")
  }

  protected class CrudRepository[T <: AbstractTable[_] with GenericRepository#GenericTable](private val tableQuery: TableQuery[T]) {
    def all = db.run(tableQuery.to[List].result)
    def create(obj: T#TableElementType) = db.run(tableQuery returning tableQuery.map(_.id) += obj)
    def delete(id: Long) = db.run(tableQuery.filter(_.id === id).delete)
  }
}

错误:

value delete is not a member of slick.lifted.Query[T,T#TableElementType,Seq]

我已经google了很多,但没有解决方案适合我。例如,我尝试更换&#39; import driver.api。&#39;使用&#39; import slick.driver.H2Driver.api。&#39;没有任何运气。

我正在使用Scala 2.11.7和Play-slick 2.0.2以及Play 2.5。

1 个答案:

答案 0 :(得分:2)

编辑:从您粘贴的代码中我现在看到了您的问题。

只需将您的定义更改为(我只更改了类型参数):

protected class CrudRepository[E, T <: Table[E] with GenericRepository#GenericTable](private val tableQuery: TableQuery[T]) {
    def all = db.run(tableQuery.to[List].result)
    def create(obj: T#TableElementType) = db.run(tableQuery returning tableQuery.map(_.id) += obj)
    def delete(id: Long) = db.run(tableQuery.filter(_.id === id).delete)
  }

其中Tableslick.relational.RelationalProfile.API.Table

然后以下列方式实例化您的CrudRepository

val crud = new CrudRepository[Redirect,RedirectsTable](Redirects)

Otherthan,它看起来很好。