Search code examples
scalaslick

How to group results with Slick


Let's say I have those two models:

case class Department(id: Long, title: String)
case class Program(id: Long, departmentId: Long, title: String)

And two TableQuery, departments and programs, based on Table mapped to those case classes respectively.

I would like to make a query returning a Seq[(Department, Seq[Program])], where I have a list of departments with their corresponding programs.

I started like this:

val query = 
  (departments join programs on ((d, p) => d.id === p.departmentId))
    .groupBy {...}

But what ever I put in the group by clause just doesn't make sense.

please help.


Solution

  • How about this?

    val query = departments.join(programs).on(_.id === _.departmentId)
            .result
            .map(_.groupBy(_._1.id))
    
    db.run(query)