0

I want to get value from parent table and all corresponding rows in second table. Is it possible in Slick

For example, this code,

db.run(val crossJoin = for {
  (c, s) <- coffees join suppliers
} yield (c, s))

return Future[Seq[(Coffee, Supplier)]], but how I cat get Future[Seq[(Supplier, Seq[Coffee])]]

  • https://stackoverflow.com/questions/31305625/translate-nested-join-and-groupby-query-to-slick-3-0 is same problem. Answer compiled, but get run time error – Adel Chepkunov Aug 11 '19 at 04:53

1 Answers1

1

To get Future[Seq[(Supplier, Seq[Coffee])]] you would need to group by supplier:

crossJoin.groupBy(_._2)

But you need to follow a groupBy with a map that tells how to aggregate what wasn't used in the groupBy; you need to tell how to aggregate the coffees into a Seq:

crossJoin.groupBy(_._2).map { case (supplier, coffees) => (supplier, aggregate(coffees) }

There's no aggregate method to achieve this in Slick, and some RDBMSs don't have it either.

Maybe you have to stick to the usual scala collections api, after you get the Future:

db.run(suppliers.join(coffees).result).map(_
  .groupBy(_._1)
  .view.mapValues(_.map(_._2))
  .toSeq
)