Reputation: 1213
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
Upvotes: 0
Views: 1943
Reputation: 1244
Roman's answer is correct, but with one caveat...
You must put the implicit MappedColumnType above the Table definition if you put it in the same file, or put it in another file and import it. If you don't do this then the implicit resolution fails to find it.
you can see this in this other StackOverflow question
So to be pedantically correct you should do this:-
object implicitDateTimeConverters {
implicit val JodaDateTimeMapper = MappedColumnType.base[DateTime, Timestamp](
dt => new Timestamp(dt.getMillis),
ts => new DateTime(timestamp.getTime())
)
}
import implicitDateTimeConverters._
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)
}
Upvotes: 0
Reputation: 5699
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())
)
Upvotes: 1