在 Slick 3.0.2 中过滤自定义列的选项

Filtering on an option of a custom column in Slick 3.0.2

我在编译以下示例时遇到了一些问题。

import scala.slick.driver.MySQLDriver.simple._

case class Age(value: Int)
case class User(id: Long, age: Option[Age])

object Dao {
  implicit val ageColumnType: TypedType[Age] = MappedColumnType.base[Age, Int](_.value, Age(_))

  class UserTable(tag: Tag) extends Table[User](tag, "users") {
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
    def age = column[Option[Age]]("age")
    def * = (id, age) <> (User.tupled, User.unapply)
  }

  val users = TableQuery[UserTable]
  def byId(id: Long)(implicit session: Session): Option[User] = {
    users.filter(_.age === Some(Age(21))).firstOption
  }
}

但是编译器失败并出现以下错误:

Example.scala:16:28: value === is not a member of slick.lifted.Rep[Option[Age]]

正确的方法是使用 OptionColumnExtensionMethods 还是什么?然而,奇怪的是 TypedType[Option[T]] 的类型 类 不会在这里出现。


这是我挖掘的一些其他资源的列表,但其中 none 似乎使用 mappedColumnType 处理围绕自定义列类型的容器类型。

弄明白了,觉得值得在这里发布。


下一行的类型签名过于宽泛。

implicit val ageColumnType: TypedType[Age]

显然,隐式范围不再包含正确的证据来推断 filter 查询中所需的各种列运算符,包括 === 方法。相反,我只需要一个更具体的类型:

implicit val ageColumnType: BaseColumnType[Age]

根据对原始问题的评论,==? 解决方案在进行此更改后也适用。谢谢!