How to use DateTime in Slick2.0?
Asked Answered
C

2

8

I want to use DateTime in Slick 2.0 model. I use jodatime:

I added the dependencies in Build.scala:

   "joda-time" % "joda-time"    % "2.3",
   "org.joda"  % "joda-convert" % "1.6"

I then do:

 class Comment(tag:Tag) extends Table[(Long, Int, Int, String, Int, DateTime)](tag,"Comment"){
  def id=column[Long]("ID", O.PrimaryKey)
  def rate=column[Int]("rate")
  def sub=column[Int]("subject")
  def content=column[Int]("cotent")
  def user_ID=column[Int]("user")
  def time=column[DateTime]("time")   //-----------an error here
  def * = (id, rate,sub, content, user_ID, time)
}

the error is:

 could not find implicit value for parameter tm: scala.slick.ast.TypedType[org.joda.time.LocalDate]

I added joda-convert jar but it doesn't seems to work. How to add a DateTime in a Slick model class?

Calton answered 22/3, 2014 at 13:56 Comment(1)
How does the accepted solution helps you? Because, I am facing the same issue. But the below solution didn't solve my issue. Can you please help me to fix the issue? Can you please look at the issue https://mcmap.net/q/1322953/-play-framework-2-5-scala-oauth2-provider-issue/1584121. Thanks :(Pasquale
P
7

Take a look at https://github.com/tototoshi/slick-joda-mapper or you must create your own type mapper

Pinery answered 22/3, 2014 at 14:34 Comment(1)
This is the best answerMum
Z
14

The other answer mentioned the library, but if you want to just create your own mapper and have it in the class, this is one such example:

implicit def dateTime =
    MappedColumnType.base[DateTime, Timestamp](
      dt => new Timestamp(dt.getMillis),
      ts => new DateTime(ts.getTime)
    )

Source: Paul Coghlan (@paulcoghlan)'s comment on Gist here https://gist.github.com/dragisak/4756344#comment-1211671

Zahara answered 13/5, 2015 at 15:27 Comment(1)
Considering simplicity this should be the better answer.Nowadays
P
7

Take a look at https://github.com/tototoshi/slick-joda-mapper or you must create your own type mapper

Pinery answered 22/3, 2014 at 14:34 Comment(1)
This is the best answerMum

© 2022 - 2024 — McMap. All rights reserved.