Reputation: 153
I was successfully mapped case classes w/o Optional fields to Postgres db via class that extends Table. Now I need to use case class with Optional[String] and Optional[DateTime] fields.
I has found how to declare mapping for it:
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)
}
Code compiles well but during runtime I get exception:
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)
What shall I do to make it work?
Upvotes: 1
Views: 788
Reputation: 15783
The columns must be defined as Option
s too:
def resolutionName = column[Option[String]]("resolutionName")
def resolutionDate = column[Option[DateTime]]("resolutionDate")
Also you can avoid the .?
in the projections function since that values are already mapped as options:
def * = (id, key, resolutionName, resolutionDate) <> (Issue.tupled, Issue.unapply)
Upvotes: 3