Camille Wanty
Camille Wanty

Reputation: 86

Slick 3.0.0 - How to sortBy on a query with joinLeft

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,

Upvotes: 6

Views: 2809

Answers (4)

Majid Hosseini
Majid Hosseini

Reputation: 1096

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(""))

Upvotes: 1

Bartosz Bąbol
Bartosz Bąbol

Reputation: 81

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)

Upvotes: 6

Richard
Richard

Reputation: 59

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)

Upvotes: 0

Riu Mike
Riu Mike

Reputation: 51

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))

Upvotes: 0

Related Questions