Consider the object of the Favorites table below, we want to write a query to find the Favorites by their type (defined below). We also defined Typemapper to map FavoriteType to String for the database
import scala.slick.driver.PostgresDriver.simple._ //Other imports have been omitted in this question object Favorites extends Table[Favorite]("favorites") { // Convert the favoriteTypes to strings for the database implicit val favoriteMapping: TypeMapper[FavorietType] = MappedTypeMapper.base[FavorietType, String]( favType => FavorietType.values.find(_ == favType).get.mapping, mapping => FavorietType.values.find(_.mapping == mapping).get ) def favoriteType = column[FavoriteType]("type") //other columns here
This is the query I want to write (however it does not compile)
def queryByFavoriteType(ftype : FavoriteType)(implicit s: Session) = { for( f <- Favorieten if f.favoriteType === ftype ) yield f } }
Here I defined different FavoriteType objects (this is outside the Favorieten object)
sealed case class FavorietType(mapping: String) { override def toString = mapping.capitalize } object FavoriteType { object Exam extends FavoriteType("examen") object Topic extends FavoriteType("onderwerp") object Paper extends FavoriteType("profielwerkstuk") val values = Seq(Exam , Topic , Paper ) }
The problem I have here is that the request does not compile: value === is not a member of scala.slick.lifted.Column[models.gebruiker.FavorietType]
It seems that === cannot be used to compare a custom type, is that true? Is there an alternative way to do this?
Edit
Related problem: before I got my TypeMapper without an explicit type, it was defined as implicit val favoriteMapping = MappedTypeMapper.base[FavorietType, String]( ...
When I write a request that will compare the FavoriteType.Exam file (for example), for example
def queryByFavoriteExam()(implicit s: Session) = { for(f <- Favorieten if f.favorietType === FavorietType.Exam) yield f }
This will lead to an error could not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[models.gebruiker.FavorietType.Exam.type] The solution for this is the same as the one below