Sorting a table by date in Slick 3.1.x

504 views
Skip to first unread message

ps33...@gmail.com

unread,
May 25, 2016, 9:55:23 PM5/25/16
to Slick / ScalaQuery

I have the following Slick class that includes a date:

import java.sql.Date
import java.time.LocalDate

class ReportDateDB(tag: Tag) extends Table[ReportDateVO](tag, "report_dates") {

  def reportDate = column[LocalDate]("report_date")(localDateColumnType)

  def * = (reportDate) <> (ReportDateVO.apply, ReportDateVO.unapply)

  implicit val localDateColumnType = MappedColumnType.base[LocalDate, Date](
    d => Date.valueOf(d),
    d => d.toLocalDate
  )

}


The class works fine. However when I attempt to sort the table by date:

    val query = TableQuery[ReportDateDB]
    val action = query.sortBy(_.reportDate).result


I get the following compilation error in the sortBy statement:

  • not enough arguments for method sortBy: (implicit evidence$2: slick.lifted.Rep[java.time.LocalDate] ⇒ slick.lifted.Ordered)slick.lifted.Query[fdic.ReportDateDB,fdic.ReportDateDB#TableElementType,Seq]. Unspecified value parameter evidence$2.

  • No implicit view available from slick.lifted.Rep[java.time.LocalDate] ⇒ slick.lifted.Ordered.

How to fix this? the sortBy needs another argument? an implicit function needs to be created?

Mirko Stocker

unread,
May 26, 2016, 1:46:48 AM5/26/16
to scala...@googlegroups.com
Hi,

> No implicit view available from slick.lifted.Rep[java.time.LocalDate] ⇒
> slick.lifted.Ordered.
>
> How to fix this? the sortBy needs another argument? an implicit function
> needs to be created?

It's a bit misleading, but you don't need to do anything else than making your
"implicit val localDateColumnType" available where you run the query. For
example, this will work:


implicit val localDateColumnType = MappedColumnType.base[LocalDate, Date](
d => Date.valueOf(d),
d => d.toLocalDate)

val query = TableQuery[ReportDateDB]
val action = query.sortBy(_.reportDate).result

I'm not sure where the best place to put this is, but I usually put all these
conversions in a package object.

Cheers

Mirko

--
Mirko Stocker | mi...@stocker.email
Work: http://ifs.hsr.ch | http://infoq.com
Personal: http://misto.ch | http://twitter.com/m_st

ps33...@gmail.com

unread,
May 26, 2016, 8:48:18 AM5/26/16
to Slick / ScalaQuery, mi...@stocker.email
Thanks, making available the implicit function in the client code solved the problem
Reply all
Reply to author
Forward
0 new messages