Slick and filtering by Option columns

后端 未结 1 940
南旧
南旧 2020-12-30 03:38

I\'m trying to filter against an optional date column with Scala Slick 1.0.1.

It may be I just don\'t see it, but I\'ve got a table that looks something like this:

1条回答
  •  时光说笑
    2020-12-30 04:19

    This is not pretty (the part about null.asInstanceOf), but I think it will work. I got that idea from an old Scala Query post, so I don't know if slick ever put something better in for that, but when I looked at the resulting selectStatement from the query, it looked correct:

    val now = new java.sql.Date(System.currentTimeMillis())
    val query = for {
      role <- UserRole
      if (role.endDate === null.asInstanceOf[Option[java.sql.Date]] || role.endDate > now)
    } yield role
    

    EDIT

    Thanks to the comment by @MartinKolinek, this code will also work and is much cleaner and probably the better way to do things:

    val now = new java.sql.Date(System.currentTimeMillis())
    val query = for {
      role <- UserRole
      if (role.endDate.isNull || role.endDate > now)
    } yield role
    

    0 讨论(0)
提交回复
热议问题