Slick table does not accept type parameter
I am playing with slick 3.0.0 new DBIO api but I am having problems with generics.
With this code:
import scala.concurrent._
import slick.driver.MySQLDriver.api._
import slick.lifted.AbstractTable
object Crud {
def fetchById[T: AbstractTable[R], R](db: Database, table: TableQuery[T], id: Int, rowId: T => Rep[Int]): Future[Option[T]] = {
val fetchByIdQuery: Query[T, R, Seq] = table.filter(row => rowId(row) === id)
db.run(fetchByIdQuery.result)
}
}
The compiler says that AbstractTable does not take any type parameters. I don't understand this with the declaration as shown here https://github.com/slick/slick/blob/3.0/slick/src/main/scala/slick/lifted/AbstractTable.scala .
slick.lifted.AbstractTable[R] does not take type parameters
def fetchById[T: AbstractTable[R], R](db: Database, table: TableQuery[T], id: Int, rowId: T => Rep[Int]): Future[Option[T]] = {
I suspect this is also the cause of this error.
type mismatch;
found : slick.lifted.Query[T,T#TableElementType,Seq]
required: slick.driver.MySQLDriver.api.Query[T,R,Seq]
(which expands to) slick.lifted.Query[T,R,Seq]
val fetchByIdQuery: Query[T, R, Seq] = table.filter(row => rowId(row) === id)
Any advice?
+3
source to share
1 answer
Try it like this.
T <: AbstractTable[R] // instead of T : AbstractTable[R]
to make it look below
Object Crud {
def fetchById[T <: AbstractTable[R], R](db: Database, table: TableQuery[T], id: Int, rowId: T => Rep[Int])
: Future[Option[T#TableElementType]] = {
val fetchByIdQuery = table.filter(row => rowId(row) === id)
db.run(fetchByIdQuery.result.headOption)
}
}
+1
source to share