Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slick filter or where no longer support logical operations?

I use slick 2.0.2 and I just want to do a simple filter or use a where sub-statement, I just want to do the logical operations like "and", "or" and "not" inside the filter :

val subjectdata = TableQuery[SubjectTable]
...
subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId.userId)).list()

and get error:

[error] G:\testprojects\slickplay\app\controllers\ShopController.scala:89: Cannot perform option-mapped operation
[error]       with type: (Long, String) => R
[error]   for base type: (Long, Long) => Boolean
[error]     subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId
.userId)).list()
[error]
                           ^

In slick 1.0.1 I can do:

val results = Query(TableClass)
.filter(r => r.isNull || r.expires > new Timestamp(DateTime.now().getMillis()))
.list

I want to do something similar on TableQuery in Slick2. How to do it?

like image 610
user504909 Avatar asked Dec 26 '22 08:12

user504909


1 Answers

One thing to know is that Slick's operations are more strict about types than Scala's. Both operands have to have the same base type, optionally wrapped in an Options. So comparing a Double to Double or an Option[Double] is ok, but comparing it to an Int will give you such a compile time warning. The error message hints a bit you towards the problem

[error] G:\testprojects\slickplay\app\controllers\ShopController.scala:89: Cannot perform option-mapped operation
[error]       with type: (Long, String) => R
[error]   for base type: (Long, Long) => Boolean
[error]     subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId
.userId)).list()

In (Long, String) => R you see that the arguments do not have matching types and that the return type cannot be determined. So I assume either id or rs.user.get.identityId is a String. Turn is into an Int using .toInt. Alternatively you can convert the db-side value using .asColumnOf[String].

like image 149
cvogt Avatar answered Jan 31 '23 10:01

cvogt