我正在尝试使用具有用户定义类型(枚举)的列的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)
}
我已经搜索了其他一些问题,但它们要么不是光滑的2.x.x,要么不涉及枚举类型。
我的问题是,我是否需要在枚举类型的某处定义===运算符,或者使用我缺少的当前slick 2.0.0功能是否有更简单的方法?
由于
答案 0 :(得分:17)
我认为在使用===运算符时,您需要在作用域中使用隐式类型映射器。你应该把
implicit val credentialTypeColumnType = MappedColumnType.base[CredentialType, String](
{ c => c.toString },
{ s => CredentialType.withName(s)}
)
在您创建查询时可见的位置。