Slick and filtering by Option columns

旧巷老猫 提交于 2019-11-30 06:40:30

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!