title: Slick 3.0 Array Aggregation Alternative
body: <p>There is already a topic about array aggregation found here:</p>
  
  <p><a href="https://stackoverflow.com/questions/28019021/support-of-postgresql-specific-array-agg-function-in-scala-frameworks">Support of PostgreSQL specific array_agg function in scala frameworks?</a></p>
  
  <p>However, since it's not currently supported, I'm finding it hard to do a few things with slick. Heres an simple example.</p>
  
  <p>Say for example, that I run a business hosting parties.
  Each party there are guests, hosts:</p>
  
  <pre><code>Party(id: Option[Int], name: String)
  Guest(id: Option[Int], name: String)
  Host(id: Option[Int], name: String)
  
  PartyGuests(partyId, guestId: Int)
  PartyHosts(partyId, hostId: Int)
  </code></pre>
  
  <p>And I want a table of all the parties, displaying all the guests and hosts like this:</p>
  
  <pre>
  PartyId     |Guests                 |Hosts
  ------------|-----------------------|------
  1           |Adam, Julie, Jack      |Tom, Jamie, John
  2           |Joe, Jim, Jake         |Tom, Tony, James
  3           |Amy, Sally             |Jenny
  </pre>
  
  <p>My solutions is this:</p>
  
  <pre><code>// The map is: partyId -&gt; (guestNamesList, hostNamesList)
  val partyTable: Map[Int, (List[String], List[String])] = (for {
    party &lt;- db.run(parties.result.list)
    guest &lt;- db.run(partyGuests
      .filter(_.partyId === party.id)
      .flatMap(_.guest)
      .result.list)
    host &lt;- db.run(partyHosts
      .filter(_.partyId === party.id)
      .flatMap(_.host)
      .result.list)
  } yield (party, guest, host))
    .groupBy { case (party, guest, host) =&gt;
      party.id
    }
    .mapValues { values =&gt;
      val myGuests = values.map(_._2).distinct.map(_.name)
      val myHosts = values.map(_._3).distinct.map(_.name)
      (myGuests, myHosts)
    }
  </code></pre>
  
  <p>However this solution is very space consuming on large data sets. Does anyone know of a better solution?</p>