Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use java.util.Date with Slick 3.1.0-M1

Tags:

mysql

scala

slick

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

like image 855
agusgambina Avatar asked Dec 09 '22 01:12

agusgambina


1 Answers

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.

like image 199
Allen Chou Avatar answered Dec 11 '22 09:12

Allen Chou