Slick 不知道如何映射给定类型 - Option(DateTime.now)

Slick does not know how to map the given types - Option(DateTime.now)

我正在尝试这样做(在 Play Framework 上):

db.run(users.filter(_.id === id).map(_.deleted).update(Option(DateTime.now)))

但是它抛出一个编译错误:

No matching Shape found. Slick does not know how to map the given types. Possible causes: T in Table[T] does not match your * projection, you use an unsupported type in a Query (e.g. scala List), or you forgot to import a driver api into scope. Required level: slick.lifted.FlatShapeLevel Source type: slick.lifted.Rep[Option[org.joda.time.DateTime]] Unpacked type: T Packed type: G

Slick 3.0.3 版本。 我该如何修复这个错误?

class UserTable(tag: Tag) extends Table[User](tag, "user") {

  def id = column[Int]("id")

  def name = column[String]("name")

  def age = column[Int]("age")

  def deleted = column[Option[DateTime]]("deleted")

  override def * =
    (id, name, age, deleted) <> ((User.apply _).tupled, User.unapply)
}

case class User(
  id: Int = 0,
  name: String,
  age: Int,
  deleted: Option[DateTime] = None
)

当您定义 table 时,DateTime 的范围内有一个列类型映射器。像

implicit val dtMapper = MappedColumnType.base[org.joda.time.DateTime, Long](
  dt => dt.getMillis,
  l => new DateTime(l, DateTimeZone.UTC)
)

例如。此映射器还必须在您构建查询的位置的范围内,否则 Slick 将不知道如何将您编写的内容转换为 SQL 查询。

如果您想将查询与 table 分开,您可以导入映射器,或者在您定义 UserTable 的同一文件中定义查询。一种常见的模式是将 table 包装在特征中,比如 UserRepository,并在该特征中定义 table 和查询。

See this page 了解有关隐式范围如何工作的更多信息。