netcrash netcrash - 5 months ago 90
Scala Question

implicit conversion of org.joda.time.DateTime to java.sql.Timestamp from a model to a slick Table[]

I have a model that takes a org.joda.time.DateTime
yet I'm passing a java.sql.Timestamp that's use by the slick object Table[], I tried to use implicit conversion but it does not work

import models.Carros.convertDateToTimestamp // this has a parameter DateTime and a return Timestamp
def * = (id, name, year, description, img, keywords, state, model, datein) <>
((Carro.apply _).tupled, Carro.unapply) // so here when unpacking shouldn't the implicit conversion do it's job?

The error shown is here:

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. Or you use an unsupported type in a Query (e.g. scala
List). Required level: slick.lifted.FlatShapeLevel Source type:
(slick.lifted.Rep[Option[Long]], slick.lifted.Rep[String],
slick.lifted.Rep[Int], slick.lifted.Rep[String],
slick.lifted.Rep[String], slick.lifted.Rep[String],
slick.lifted.Rep[String], slick.lifted.Rep[Long],
slick.lifted.Rep[java.sql.Timestamp]) Unpacked type: (Option[Long],
String, Int, String, String, String, String, Long,
org.joda.time.DateTime) Packed type: Any


You will need to declare the type of datebin as org.joda.DateTime, not as java.sql.Timestamp:

class CarroTable extends Table[Carro](tag: "carro") {
  val datebin = column[org.joda.DateTime]("datebin")

  def * = (id, name, year, description, img, keywords, state, model, datein) <> (Carro.tupled, Carro.unapply)


Then make sure you have an implicit type mapper in place:

implicit val JodaDateTimeMapper = MappedColumnType.base[DateTime, Timestamp](
  dt => new Timestamp(dt.getMillis),
  ts => new DateTime(timestamp.getTime())