Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala slick query comparison of a custom user type (enumeration) gives error

I'm trying to use Slick with a column that has a user defined type (an enumeration). All is working until I try to write a query that uses the column.

When compiling I get an error on 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

The enumeration code is here:

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

The table definition is here:

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've googled a number of other questions but they're either not for slick 2.x.x or do not involve Enumeration types.

My question is, do I need to define the === operator somewhere for enumeration types, or is there a simpler way using current slick 2.0.0 functionality that I'm missing?

Thanks

like image 670
SkydivingCoder Avatar asked Feb 09 '14 03:02

SkydivingCoder


1 Answers

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

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

somewhere where it is visible when you are creating the query.

like image 76
Martin Kolinek Avatar answered Oct 19 '22 18:10

Martin Kolinek