Scala comparing a custom query of a custom type (enumeration) gives an error

I am trying to use Slick with a column that has a user defined type (enumeration). Everything works until I try to write a query that uses a column.

When compiling, I get an error message using the following method:

def findCredentials(credentialType:CredentialType)(implicit session: Session): List[Credential] = { val query = for { c <- credentials if c.credentialType === credentialType } yield c query.list } 

Here is the error:

 [error] ... value === is not a member of scala.slick.lifted.Column[models.domain.enumeration.CredentialType.CredentialType] [error] c <- credentials if c.credentialType === credentialType 

Listing code here:

 object CredentialType extends Enumeration { type CredentialType = Value val Password, Token = Value } 

Table definition:

 case class Credential(id: Long, userId: Long, credentialType: CredentialType) class Credentials(tag: Tag) extends Table[Credential](tag, "credential") { implicit val credentialTypeColumnType = MappedColumnType.base[CredentialType, String]( { c => c.toString }, { s => CredentialType.withName(s)} ) def id = column[Long]("id", O.PrimaryKey, O.AutoInc) def userId = column[Long]("user_id") def credentialType = column[CredentialType]("type") def * = (id, userId, credentialType) <> (Credential.tupled, Credential.unapply) } 

I looked at a number of other questions, but they are either not for slick 2.xx or are not related to enumeration types.

My question is: do I need to define the === operator somewhere for enum types, or is there an easier way to use the current slick 2.0.0 functionality that I skip?

thanks

+8
comparison scala enumeration slick
source share
1 answer

I think you need an implicit mapper type in scope when using the === operator. You have to put

 implicit val credentialTypeColumnType = MappedColumnType.base[CredentialType, String]( { c => c.toString }, { s => CredentialType.withName(s)} ) 

somewhere where it is visible when you create a request.

+15
source share

All Articles