I want to do something like this in Slick 2.0.x: select users with age <= maxAge or if maxAge is None, select all users (limited by a number)
. I tried this:
private def ageQuery(maxAge: Column[Option[Int]], limit: Column[Int]) = {
Users.filter(user => maxAge.map(_ >= user.age).getOrElse(true)).take(limit)
}
val compiledAgeQuery = Compiled(ageQuery _)
I am running into following issues:
limit
is aColumn[_]
, butUsers.take
expects anInt
. If I change itlimit
toInt
then I can't use theCompiled
macro anymore since that expects onlyColumn[_]
This snippet itself does not compile:
user => maxAge.map(_ >= user.age).getOrElse(true)
I have found following solution to your problem:
This is what I did:
limit
parameter toscala.slick.lifted.ConstColumn[Long]
. Now it satisfies bothtake
method andCompiled
macromaxAge
, and whenmaxAge
isNone
then it returns all users younger than 1000 years. It is workaround but it works good (assuming that noone is older than 1000 years)