Slick: dynamic sortBy in a query with left join

前端 未结 1 843
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 15:27

This is a problem derived from another question. I need to be able to dynamically pass a column to be sorted on in a Slick query which has a left join. The problem in this p

相关标签:
1条回答
  • 2021-01-15 16:16

    .? is implemented using <> which prevents you from later accessing members. So you need to apply the sorting before you do the .?

        val data = for {
            (computer, company) <- Computer.where(_.name like filter) leftJoin
                Company on (_.companyId === _.id)
        } yield (computer, company) // <- no .?
    
        val sortedData = orderBy match {
            case 2 => data.sortBy(_._1.name) //Works ok, column from a primary table
            case 3 => data.sortBy(_._2.name) //Error "Cannot resolve symbol name", because table is optional
        }
    
        val optionalJoinData = sortedData.map{
          case (computer, company) => (computer, company.?)
        } // <- do .? last
    
    0 讨论(0)
提交回复
热议问题