【问题标题】:Scala slick query comparison of a custom user type (enumeration) gives error自定义用户类型(枚举)的 Scala 灵活查询比较给出错误
【发布时间】:2014-02-09 03:48:18
【问题描述】:

我正在尝试将 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 功能?

谢谢

【问题讨论】:

    标签: scala comparison enumeration slick


    【解决方案1】:

    我认为在使用 === 运算符时,您需要在范围内使用隐式类型映射器。你应该把

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

    创建查询时可见的地方。

    【讨论】:

    • 谢谢你,做到了...我试图给你投票,但我还没有足够的积分来做这件事:-)
    猜你喜欢
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多