How to create custom column mapping in Slick 3.1.1

652 views
Skip to first unread message

Krille

unread,
Feb 16, 2016, 8:38:39 AM2/16/16
to Slick / ScalaQuery
Hi,

I'm using Slick 3.1.1 and I wish to use joda time api instead of java.sql. Currently I have this mapping code:

trait ColumnTypeMappings {

  import slick.driver.MySQLDriver.api._
  import scala.language.implicitConversions

  val localTimeFormatter = DateTimeFormat.forPattern("HH:mm:ss")
  val javaTimeFormatter = new SimpleDateFormat("HH:mm:ss")

  implicit val dateColumnType = MappedColumnType.base[LocalDate, Date](
    ld => new java.sql.Date(ld.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis),
    d  => new LocalDateTime(d.getTime).toLocalDate
  )

  implicit val timeColumnType = MappedColumnType.base[LocalTime, Time](
    lt => new java.sql.Time(javaTimeFormatter.parse(lt.toString(localTimeFormatter)).getTime),
    t  => new LocalTime(t.getTime)
  )

  implicit val timestampColumnType = MappedColumnType.base[DateTime, Timestamp](
    dt => new java.sql.Timestamp(dt.getMillis),
    ts => new DateTime(ts.getTime, DateTimeZone.UTC)
  )

}

But this won't compile: could not fint implicit value for evidence parameter of type slick.driver.MySQLDriver.BaseColumnType[java.sql.Date]

Any help would be appreciated.

Krille

unread,
Feb 16, 2016, 9:05:03 AM2/16/16
to Slick / ScalaQuery
I renamed the implicit values, since in slick.driver.JdbcTypesComponent there is a trait called ImplicitColumnTypes which contains some defaults which I had named exactly the same. Now it compiles fine. Now I wonder how to import this into the generated Tables.scala to make it work. I tried with:

package mypackage

object Tables extends {
  val profile
= slick.driver.MySQLDriver
} with Tables

trait
Tables {
  val profile
: slick.driver.JdbcProfile
 
import profile.api._
 
import mypackage.ColumnTypeMappings

 
// ... ++ rest of auto generated code
}


But this results in lots of errors like: could not find implicit value for parameter tt: slick.ast.TypedType[org.joda.time.DateTime]

Message has been deleted

Roy Phillips

unread,
Feb 18, 2016, 8:01:54 AM2/18/16
to Slick / ScalaQuery
Did you try:
import mypackage.ColumnTypeMappings._ 
Reply all
Reply to author
Forward
0 new messages