6

This question is related to another. I'm also trying to sort on a query with a joinLeft but in slick 3.0.0. And as the Option Rep are automatically lifted how would I do the exact same thing ?:

def list(filter: String, orderBy: Int):Future[Seq[(Computer, Option[Company])]] = {
    val initialQuery = for {
        (computer, company) <- Computer.filter(_.name like filter) leftJoin 
            Company on (_.companyId === _.id)
    } yield (computer, company)

    val sortedQuery = orderBy match {
        case 2 => initialQuery.sortBy(_._1.name) //Works ok, column from a primary table
        case 3 => initialQuery.sortBy(_._2.map(_.name)) //could not find implicit value for parameter ol: slick.lifted.OptionLift[slick.lifted.ColumnOrdered[String],slick.lifted.Rep[Option[QO]]]
    }
    db.run(sortedQuery.result)
}

Thanks,

Community
  • 1
  • 1
Camille Wanty
  • 76
  • 1
  • 5

4 Answers4

6

I suppose that missing parenthesis is just a typo. I had this problem recently when I was specifying the sort direction in the wrong place, using your example:

case 3 => initialQuery.sortBy(_._2.map(_.name.asc))

It should be:

case 3 => initialQuery.sortBy(_._2.map(_.name).asc)
ThaDon
  • 7,826
  • 9
  • 52
  • 84
1

I had this problem too. I had a joinLeft and I want to order on a boolean column. You must be decide when Its join is empty what value you want replace it.

for example:

initialQuery.sortBy(_._2.map(_.name).getOrElse(""))
Majid Hosseini
  • 1,098
  • 10
  • 17
0

Are you sure you copied the correct code?

case 3 => data.sortBy(_._2.map(_.name) //could not find implicit value for parameter

Missing a ")"

Should be

case 3 => data.sortBy(_._2.map(_.name))
Riu Mike
  • 51
  • 5
0

You could put the fields into the result set. For example:

 val initialQuery = for {
        (computer, company) <- Computer.filter(_.name like filter) leftJoin Company on (_.companyId === _.id)
        } yield (computer, company, company.map(_.name))

    val sortedQuery =  orderBy match {
        case 2 => initialQuery.sortBy(_._1.name) 
        case 3 => initialQuery.sortBy(_._3)
    }

    db.run(sortedQuery.map(v => (v._1, v._2).result)
ThaDon
  • 7,826
  • 9
  • 52
  • 84
Richard
  • 49
  • 1
  • 4