Note that there are some explanatory texts on larger screens.

plurals
  1. POSLICK How to define bidirectional one-to-many relationship for use in case class
    primarykey
    data
    text
    <p>I am using SLICK 1.0.0-RC2. I have defined the following two tables Directorate and ServiceArea where Directorate has a one to many relationship with ServiceArea </p> <pre><code>case class Directorate(dirCode: String, name: String) object Directorates extends Table[Directorate]("DIRECTORATES") { def dirCode = column[String]("DIRECTORATE_CODE", O.PrimaryKey) def name = column[String]("NAME") def * = dirCode ~ name &lt;&gt; (Directorate, Directorate.unapply _) } case class ServiceArea(areaCode: String, dirCode: String, name: String) object ServiceAreas extends Table[ServiceArea]("SERVICE_AREAS") { def areaCode = column[String]("AREAE_CODE", O.PrimaryKey) def dirCode = column[String]("DIRECTORATE_CODE") def name = column[String]("NAME") def directorate = foreignKey("DIR_FK", dirCode, Directorates)(_.dirCode) def * = areaCode ~ dirCode ~ name &lt;&gt; (ServiceArea, ServiceArea.unapply _) } </code></pre> <p>To make the Directorate case class useful in my Play application form I am trying to redefine the Directorate case class to have a <em>Seq</em> of ServiceAreas that are related to that Directorate. </p> <pre><code>case class Directorate(dirCode: String, name: String, serviceAreas: Seq[ServiceArea]) </code></pre> <p>My problem is now with the Directorate table projection. I have attempted to create a method in Directorates:</p> <pre><code>def serviceAreas = (for { a &lt;- ServiceAreas if (a.dirCode === dirCode) } yield (a)).list map { case t: ServiceArea =&gt; t } </code></pre> <p>so that I can try something like</p> <pre><code>def * = dirCode ~ name ~ serviceAreas &lt;&gt; (Directorate, Directorate.unapply _) </code></pre> <p>but this cannot not work as serviceAreas only goes one way.</p> <p>It seems reasonable to me that for the Directorate case class to be a useful domain object that it should be able contain the related ServiceAreas. </p> <p>I'm wondering how I should traverse the inverse relationship so that Directorate table projection will work. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload