agusgambina agusgambina - 1 year ago 176
SQL Question

Use java.util.Date with Slick 3.1.0-M1

I am new to Slick. I am using mysql and I am trying to retrieve some datetime from the database. Here are my imports

import slick.driver.MySQLDriver.simple._
import scala.slick.driver._
import java.util.Date

and here the line of the class where the mapping is

def creationDate = column[Date]("creation_date")

But I am getting this error

could not find implicit value for parameter tt: slick.ast.TypedType[java.util.Date]

Is there a way to import a datetime from mysql to a java.util.Date without using String?

Thank you

Answer Source

The reason you can't use java.util.Date in the Column is it's not supported in Slick, see the documentation in Table Rows part.

The following primitive types are supported out of the box for JDBC-based databases in JdbcProfile (with certain limitations imposed by the individual database drivers): Date types: java.sql.Date, java.sql.Time, java.sql.Timestamp

Thus, no implicit TypedType[C] is provided.

 def column[C](n: String, options: ColumnOption[C]*)
   (implicit tt: TypedType[C]): Rep[C] = {

If you try to find the children of TypedType, you will find three time-relevant class in slick.driver.JdbcTypesComponent.

DateJdbcType for java.sql.Date
TimestampJdbcType for java.sql.Timestamp
TimeJdbcType for java.sql.Time

Also, the types defined are in line with what is stated in the documentation, three time-relevant type.

I use Timestamp with Slick 3.0 in my program as following:

import slick.driver.MySQLDriver.api._
import java.sql.Timestamp

case class Temp(creation_date: Timestamp)

class Tests(tag: Tag) extends Table[Temp](tag, "tests") {
  def creationDate = column[Timestamp]("creation_date")

  def * = creationDate <> ((creationDate: Timestamp) =>   
     Temp.apply(creationDate), Temp.unapply _)

In that way, you just have to convert Timestamp to any time-relevant type you want back and forth, but that should be no big deal.

Anyway, hope it helps.