sql-serverscalaplayframeworkslick-3.0

Scala, Play framework, Slick: db join query with filter on enumeration field


I have a Slick query with join having a filter on an enumeration field, for which I get the below compilation error:

[error] /Users/someuser/SomeApp/app/com/somePackage/SomeDAO.scala:190:84: value === is not a member of slick.lifted.Rep[com.somePackage.models.MyCustomEnum.MyCustomEnum]
[error]           myCustomEnumTbl.userId === userTbl.id && myCustomEnumTbl.selectionType === MyCustomEnum.FOOD
[error]                                                                                  ^

Code snippet:

var userQuery = for {
  (user, selection) <- Users.filter(_.id === userId).join(selectionTableDAO.getTable).on {
  (myCustomEnumTbl,userTbl) => myCustomEnumTbl.userId === userTbl.id && myCustomEnumTbl.selectionType === MyCustomEnum.FOOD
} yield (user, selection)
val userRes = db.run(userQuery.result)

What would be the right way to filter on an enum field?


Solution

  • I was able to resolve this issue by defining an implicit mapping for the enumeration field:

    implicit val CustomSelectionTypeEnumsTypesMapper = MappedColumnType.base[MyCustomEnum, String](
      e => e.toString,
      s => MyCustomEnum.withName(s)
    )