我通过扩展表的类成功地将不带可选字段的案例类映射到 Postgres 数据库。 现在我需要使用带有 Optional[String] 和 Optional[DateTime] 字段的案例类。
我已经找到了如何为其声明映射:
case class Issue(id: Int,
key: String,
...
resolutionName: Option[String],
resolutionDate: Option[DateTime],
)
case class Issues(tag: Tag) extends Table[Issue](tag, "Issues") {
// This is the primary key column:
def id = column[Int]("id", O.PrimaryKey)
def key = column[String]("key")
...
def resolutionName = column[String]("resolutionName")
def resolutionDate = column[DateTime]("resolutionDate")
def * = (id, key, resolutionName.?, resolutionDate.?) <> (Issue.tupled, Issue.unapply)
}
代码编译良好,但在运行时出现异常:
Exception in thread "main" scala.slick.SlickException: JdbcProfile has no JdbcType for type UnassignedType
at scala.slick.driver.JdbcTypesComponent$class.jdbcTypeFor(JdbcTypesComponent.scala:66)
at scala.slick.driver.PostgresDriver$.jdbcTypeFor(PostgresDriver.scala:151)
at scala.slick.driver.JdbcTypesComponent$JdbcType$.unapply(JdbcTypesComponent.scala:49)
我该怎么做才能让它发挥作用?
最佳答案
列也必须定义为 Option
:
def resolutionName = column[Option[String]]("resolutionName")
def resolutionDate = column[Option[DateTime]]("resolutionDate")
您还可以避免投影函数中的 .?
,因为该值已经映射为选项:
def * = (id, key, resolutionName, resolutionDate) <> (Issue.tupled, Issue.unapply)
关于postgresql - 斯卡拉.slick.SlickException : JdbcProfile has no JdbcType for type UnassignedType - on Option fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480788/