Scala Slick查询自定义用户类型(枚举)的比较会出现错误

10

我想在一个包含用户定义类型(枚举)的列中使用Slick。一切正常,直到我尝试编写使用该列的查询。

在编译时,我会在以下方法上收到错误:

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

这里是错误:

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

枚举代码在此处:

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

这里是表格定义:

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)
}

我已经谷歌了许多其他问题,但它们要么不适用于slick 2.x.x,要么不涉及枚举类型。

我的问题是,对于枚举类型,我是否需要在某个地方定义 === 运算符,或者是否有一种更简单的方法可以使用当前的 slick 2.0.0 功能,而我却漏掉了?

谢谢

1个回答

23

我认为在使用 === 运算符时,需要将隐式类型转换器放在作用域中。你应该将

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

在创建查询时可见的某个位置。


谢谢,成功了...我试着给你点赞,但我还没有足够的积分来做到 :-) - SkydivingCoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接