title: GroupBy on multiple objects gives [SlickException: Cannot select Path s2 in Ref s3]
body: <p>I am writing a query that calculates a possible score for a QuestionAnswer, when executing the query I get a SlickException</p>
  
  <p><strong>Info about the model</strong></p>
  
  <p>A QuestionAnswer can have several (at least one) questionAnswerPossibilities, since there are multiple ways to answer the question correctly.</p>
  
  <p>Every questionAnswerPossibility has several questionAnswerParts, in the query below we query the score per questionAnswerPossibility.</p>
  
  <p><strong>The Problematic Query</strong>
  This is the query that gives the exception</p>
  
  <pre><code>  def queryAnswersWithScore()(implicit session: Session) = {
      (for{
        ovam &lt;- OpenVraagAntwoordMogelijkheden //questionAnswerPossibility
        ovad &lt;- OpenVraagAntwoordOnderdelen if ovad.ovamId === ovam.id //questionAnswerPart
        ova &lt;- OpenVraagAntwoorden if ovam.ovaId === ova.id //questionAnswer
      } yield (ova, ovam, ovad.score))
        .groupBy{ case (ova, ovam, score) =&gt; (ova, ovam)}
        .map{ case ((ova, ovam), query) =&gt; ((ova, ovam), query.map(_._3).sum)}
    }
  </code></pre>
  
  <p>However when executing this query, I get the following exception. (it is thrown when .list is called on the query object)</p>
  
  <pre><code>lay.api.Application$$anon$1: Execution exception[[SlickException: Cannot select Path s2 in Ref s3]]
      at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.3]
      at play.api.DefaultApplication.handleError(Application.scala:383) ~[play_2.10.jar:2.1.3]
      at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$handleAction$1$2$$anonfun$apply$21.apply(PlayDefaultUpstreamHandler.scala:415) ~[play_2.10.jar:2.1.3]
      at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$handleAction$1$2$$anonfun$apply$21.apply(PlayDefaultUpstreamHandler.scala:415) ~[play_2.10.jar:2.1.3]
      at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
      at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$handleAction$1$2.apply(PlayDefaultUpstreamHandler.scala:415) ~[play_2.10.jar:2.1.3]
  scala.slick.SlickException: Cannot select Path s2 in Ref s3
      at scala.slick.compiler.FuseComprehensions.select(Relational.scala:348) ~[slick_2.10-1.0.1.jar:1.0.1]
      at scala.slick.compiler.ConvertToComprehensions$$anonfun$5.applyOrElse(Relational.scala:126) ~[slick_2.10-1.0.1.jar:1.0.1]
      at scala.slick.compiler.ConvertToComprehensions$$anonfun$5.applyOrElse(Relational.scala:119) ~[slick_2.10-1.0.1.jar:1.0.1]
      at scala.slick.ast.NodeOps$.replace(Util.scala:95) ~[slick_2.10-1.0.1.jar:1.0.1]
      at scala.slick.ast.NodeOps$.replace$extension(Util.scala:52) ~[slick_2.10-1.0.1.jar:1.0.1]
      at scala.slick.ast.NodeOps$$anonfun$replace$1$$anonfun$applyOrElse$1.apply(Util.scala:95) ~[slick_2.10-1.0.1.jar:1.0.1]
  </code></pre>
  
  <p><strong>A working, similar query</strong>
  This alternative for the query does work, the difference here is that I do not select the questionAnswer (ova) object. When I call group by on one object, everything works just fine</p>
  
  <pre><code>  def queryAnswersWithScore()(implicit session: Session) = {
      (for{
        ovam &lt;- OpenVraagAntwoordMogelijkheden //questionAnswerPossibility
        ovad &lt;- OpenVraagAntwoordOnderdelen if ovad.ovamId === ovam.id //questionAnswerPart
      } yield (ovam, ovad.score))
        .groupBy{ case (ovam, score) =&gt; ovam}
        .map{ case (ovam, query) =&gt; (ovam, query.map(_._2).sum)}
    }
  </code></pre>
  
  <p>This alternative query is not a solution for my problem, since I really want a query that selects the score for every questionAnswer, which is calculated by choosing the maximum score from the questionAnswerPossibilities for this questionAnswer</p>
  
  <h1>update</h1>
  
  <p>I updated my query and now I got it to generate sql, however the result is invalid.
  See
  <a href="https://stackoverflow.com/questions/18726742/groupby-on-multiple-objects-generates-invalid-sql-in-slick">Groupby on multiple objects generates invalid SQL in slick</a></p>