Comparing mapped value types in Slick requests

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

+4
source share
1 answer

If in doubt about Slick, go on to unit tests . After reading my matching documents in a user type and then looking at their unit tests, I got the request code for compilation, changing it to:

 def queryByFavoriteType(ftype : FavoriteType)(implicit s: Session) = { for(f <- Favorites if f.favoriteType === (ftype:FavoriteType)) yield f } 

In addition, I only imported H2Driver to compile things ( import scala.slick.driver.H2Driver.simple._ ). I assumed that you also imported some driver that you need for your db.

EDIT

My complete code example is as follows:

 import scala.slick.driver.PostgresDriver.simple._ import scala.slick.session.Session sealed case class FavoriteType(mapping: String) { override def toString = mapping.capitalize } case class Favorite(ft:FavoriteType, foo:String) object FavoriteType { object Exam extends FavoriteType("examen") object Topic extends FavoriteType("onderwerp") object Paper extends FavoriteType("profielwerkstuk") val values = Seq(Exam , Topic , Paper ) } object Favorites extends Table[Favorite]("favorites") { // Convert the favoriteTypes to strings for the database implicit val favoriteMapping = MappedTypeMapper.base[FavoriteType, String]( {favType => FavoriteType.values.find(_ == favType).get.mapping}, {mapping => FavoriteType.values.find(_.mapping == mapping).get} ) def favoriteType = column[FavoriteType]("type") def foo = column[String]("foo") def * = favoriteType ~ foo <> (Favorite.apply _, Favorite.unapply _) def queryByFavoriteType(ftype : FavoriteType)(implicit s: Session) = { for(f <- Favorites if f.favoriteType === (ftype:FavoriteType)) yield f } } 
+9
source

Source: https://habr.com/ru/post/1496144/


All Articles