自定义用户类型(枚举)的Scala slick查询比较会产生错误
发布时间:2020-12-16 09:01:31 所属栏目:安全 来源:网络整理
导读:我正在尝试使用具有用户定义类型(枚举)的列的Slick.一切正常,直到我尝试编写使用该列的查询. 编译时,我得到以下方法的错误: def findCredentials(credentialType:CredentialType)(implicit session: Session): List[Credential] = { val query = for { c -
我正在尝试使用具有用户定义类型(枚举)的列的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功能是否有更简单的方法? 谢谢 解决方法
我认为在使用===运算符时,您需要在作用域中使用隐式类型映射器.你应该把
implicit val credentialTypeColumnType = MappedColumnType.base[CredentialType,{ s => CredentialType.withName(s)} ) 在创建查询时可见的位置. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |