Error in sorting in slick

(sowmya) #1


1.  var query = TableQuery[AuthProfiles]

2.    sort.foreach {

3.     sortTuple =>

4.        val (sortColumn, sortDirection) = sortTuple

5.        query = query.sortBy(sortColumn match {

6.          case "empIds" => if (sortDirection == Desc) _.pss_employeeId.desc else _.pss_employeeId.asc

7.          case "empnames" => if (sortDirection == Desc) _.pss_userName.desc else _.pss_userName.asc

8.          case _ => if (sortDirection == Desc) _.pss_userName.desc else _.pss_userName.asc

9.        })

10.    }


If I assign a query.sortBy(…) to query at line number(5). I am getting compile time error saying

that Query cannot be assigned to a TableQuery. So how this can be resolved.

Thank you

(Ivano Pagano) #2

Try defining

var query: Query[_, _, _] = TableQuery[AuthProfiles]

If it works, then the issue is that without this “type annotation”, the inferred type of query would be TableQuery[YourTable] which is more specific than a Query[...]
Hence the re-assignment will fail in the foreach as the result of sortBy is another subtype of Query[...]

As a design feedback, it’s pretty customary in Scala to use immutable references (i.e. val query = ...) and then assign the result of .sortBy to another fresh variable (e.g. val sorted = query.sortBy...)

This will by-pass your issue, as the new value will have a different type without creating any type error.