Slick groupBy Mapping

933 views
Skip to first unread message

Nicolas Esteban

unread,
Nov 7, 2013, 2:57:33 AM11/7/13
to scala...@googlegroups.com
Hi all,
I want to map a groupBy request. Here is my code :

val query = (for {
      p <- Projects
      c <- Customers if p.idcustomer === c.id
    } yield (p.id, p.name, c.name)).groupBy(_._3).list

    query.map {
      case(cusname, pbc) => (cusname, pbc.map(r2 => (r2._1, r2._2, r2._3)).list)
    }

I got this error : SlickException: Cannot select Path s23 in Ref s9

So, I google this and I understand I don't have to add ".list" to query :

val query = (for {
      p <- Projects
      c <- Customers if p.idcustomer === c.id
    } yield (p.id, p.name, c.name)).groupBy(_._3)

    query.map {
      case(cusname, pbc) => (cusname, pbc.map(r2 => (r2._1, r2._2, r2._3)).list)
    }

But then I got this error : Don't know how to unpack (scala.slick.lifted.Column[String], List[(Option[Long], String, String)]) to T and pack to G

I need help. I understand my scala.slick.lifted.Column[String] must be unpack to String. How can I do that ?

Thanks in advance !

Christopher Vogt

unread,
Nov 7, 2013, 3:45:14 PM11/7/13
to scala...@googlegroups.com
Slick currently does not support creating nested collections in results
(just like SQL does not). groupBy produces a nested collection. E.g.

(foo:Iterable[_]).groupBy(x=>x) : Iterable[ (_, Iterable[_]) ]

And thus you cannot run such a query with Slick. The problem is that we
currently cannot catch that case statically, but you get the "Cannot
select Path s23 in Ref s9" or "No type for symbol s23 found in Coll..."
in Slick 2.0.

In other cases we can catch this statically, which results in a
"Don't know how to unpack ... to T and pack to G". This means on other
words "We cannot execute a query of this type, because you used illegal
nesting or unsupported data types".

What you can do to fix this is to the operations that produce nested
results locally instead of as a query. In your case that would mean run
the .list before running the .groupBy like this:

val query = (for {
p <- Companies
c <- Computers if p.id === c.manufacturerId
} yield (p.id, p.name, c.name)).list.groupBy(_._3)

query.map {
case(cusname, pbc) => (cusname, pbc.map(r2 => (r2._1, r2._2, r2._3)))
}

Chris

Nicolas Esteban

unread,
Nov 7, 2013, 4:28:43 PM11/7/13
to scala...@googlegroups.com
Thanks Christopher, it works !
I'm a very fresh user of slick ( one day old). Thanks for the quick and efficient support !
Reply all
Reply to author
Forward
0 new messages