title: Slick.io - Handling Joined Tables
body: <p>I have a simple query in Slick (2.1) which joins two tables in a one-to-many relationship. Defined roughly as follows...</p>
  
  <pre><code>class Users( tag: Tag ) extends Table[ User ]( tag, "users" )
      // each field here...
      def * = ( id, name ).shaped &lt;&gt; ( User.tupled, User.unapply)
  
  class Items( tag: Tag ) extends Table[ Item ]( tag, "items" )
      // each field here...
  
      // foreign key to Users table
      def userId = column[ Int ]( "user_id")
      def user_fk = foreignKey( "users_fk", userId, Users )( _.id )
      def * = ( id, userId.?, description ).shaped &lt;&gt; ( Item.tupled, Item.unapply)
  </code></pre>
  
  <p>A single User can have multiple Items. The User case class I want to marshal to looks like...</p>
  
  <pre><code>case class User(id: Option[Int] = None, name:String, items:Option[List[Item]] = None)
  </code></pre>
  
  <p>I then query the database with an implicit join like this...</p>
  
  <pre><code>for{
      u &lt;- Users
      i &lt;- Items 
        if i.userId === u.id
    } yield(u, i)
  </code></pre>
  
  <p>This "runs" fine. However, the query obviously duplicates the "Users" record for each "Item" that belongs to the user giving...</p>
  
  <pre><code>List(
  (User(Some(1),"User1Name"),Item(Some(1),Some(1),"Item Description 1")), 
  (User(Some(1),"User1Name"),Item(Some(2),Some(1),"Item Description 2")))
  </code></pre>
  
  <p>Is there an elegant way of pulling the "many" part into the User case class? Whether it's Slick or Scala. What I would ideally like to end up with is...</p>
  
  <pre><code>User(Some(1),"User1Name",
      List(Item(Some(1),Some(1),"Item Description 1"),
           Item(Some(2),Some(1),"Item Description 2")))
  </code></pre>
  
  <p>Thanks!</p>