2016-12-06 32 views
1

我目前正在玩Play和play-slick。下面的代码给我一个错误value delete不是slick.lifted.Query的成员[T,T#TableElementType,Seq]

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搜索了很多,但没有办法为我工作。例如,我尝试替换'import driver.api。 '与'import slick.driver.H2Driver.api。'没有任何运气。

我正在使用Scala 2.11.7,使用play-slick 2.0.2和Play 2.5。

回答

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,它看起来不错。

+0

另外你可能会认为,如果你从'CrudRepository'方法返回'Future'是有意义的。这种方法不会允许您在交易中执行操作。 –

+0

嘿,谢谢你的回答!不幸的是,这导致了另一个错误:http://pastebin.com/k2KDemM8 显然,斯卡拉无法看到两个类的驱动程序导入是相同的。 对我来说db.run总是返回一个Future,顺便说一句。 – Magnus

+0

看看我更新的答案 - 它使您的代码编译。 –

相关问题