2016-12-06 75 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

看看我更新的答案 - 它使您的代碼編譯。 –