LoginSignup
1
2

More than 5 years have passed since last update.

quillでモデルのdatetimeにJodaTimeを使おうと思ったらdecode出来ないとコンパイラに怒られる場合の対処法

Last updated at Posted at 2016-06-08

対処

case classが例えば以下のようだった時にcreated_atやupdated_atのDateTimeがorg.joda.time.DateTimeへの変換が出来ないらしくimplicit valで変換させる方法で対処しました

case class Article (
  id: Int,
  title: String,
  url: String,
  created_at: DateTime,
  updated_at: DateTime
)

これをそのままコンパイルすると

Source doesn't know how to decode 'u.page_created_at: org.joda.time.DateTime'

のようにエラーが出てしまいました

ので

db.run()を使っているスコープ内に

  implicit val decodeDateTime = mappedEncoding[Date, DateTime](new DateTime(_))
  implicit val encodeDateTime = mappedEncoding[DateTime, Date](_.toDate)

  def findById() = {
    db.run( ArticleDao.find(1) )
  }

と書いておけばDBから取得するタイミングでJodaTimeへの変換が可能になりました

参考

getquill/quill java8 date apis #111

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2