Comparing nullable columns in Squeryl

How to compare nullable columns in Squeryl

Suppose we have a table definition like this:

case class T1 (
  id: Int,
  col1: Option[Int]
)
object FooDb extends Schema {
  val t1 = table[T1]("t1")
}

And, we want to execute a query like the following:

select * from t1 where col1 < 10

The correct statement in Squeryl is shown below:

from(FooDb.t1)( t1 =>
  where(t1.col1 lt Some(10))
  select(t1)
)

This isn’t very intuitive, and actually, it took me a lot of time to get to this answer.

Here are some that I tried and failed

The most intuitive one doesn’t compile:

  where(t1.col1 lt 10) // -> doesn't compile

This one causes NoSuchElementException:

  where(t1.col1.get lt 10) // -> NoSuchElementException

Another one that causes NoSuchElementException:

  where(t1.col1.map(_ lt 10).get) // -> NoSuchElementException

Official site should have example

I’ve found some guys that were having the same issue:

I think the Squeryl web site should have an example that uses nullable column in the where clause.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です