You need to give the user a way to specify the type of the subclass of Table[T], and to constrain it so that it has an id column method.
Incidentally you could easily generalize your question such that it has nothing to do with slick; it's a general case of abstracting over types. If you'd make stubs that replace the slick types and then give them meaningless names, it might help to see the actual problem.
--
---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/4b3ad643-c8ca-4194-b147-368ed7ace287%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
trait IdModel {
def id: Long
}
trait IdTable {
import db.RepoPostgresDriver.api._
def id: Rep[Long]
}
/** Entity class storing rows of table Users */
case class User(id: Int, email: Option[String] = None) extends IdModel
/** Table description of table users. Objects of this class serve as prototypes for rows in queries. */
class Users(_tableTag: Tag) extends Table[User](_tableTag, "users") with IdTable {
// Usual slick-generated *, ?, id email definitions here
}
/** Collection-like TableQuery object for table Users */
lazy val Users = new TableQuery(tag => new Users(tag))
abstract class BaseDao[T <: AbstractTable[E] with IdTable, E <: T#TableElementType with IdModel](currentEnv:CurrentEnv, app:Application) extends HasDatabaseConfig[RepoPostgresDriver] {
protected val dbConfig = DatabaseConfigProvider.get[RepoPostgresDriver](currentEnv.env)(app)
protected val _db = dbConfig.db
import driver.api._
val query: TableQuery[T] //<--slick.lifted.Aliases.TableQuery, overridden in subclasses e.g. val query = Users, where Users is the slick-generated lazy val Users = new TableQuery(tag => new Users(tag))
def findById(id: Long) = _db.run(query.filter(_.id === id).result.headOption) // works fine
def delete(id: Long) = _db.run(query.filter(_.id === id).delete) // doesn't work - delete method isn't found on query
}
As you can see from the comments, this approach allows me to do some things (filter, +=, update, result) but not others (delete, probably other things I have't found yet).
Although my approach is slightly different, my question is basically the same as yours:
How do I define a type bound for T so that a TableQuery[T] can not only do filter, update, etc. but also delete (and similar methods I may be missing)?