2

Another question answers how to define a one-to-many association, by defining a method in the "one" case class (Directorate) that returns a Seq of the "many" class (ServiceArea). But it doesn't address the "bidirectional" part of the question.

Using that example, I'd like to see how to traverse the association from the other direction. Instead of

case class ServiceArea(areaCode: String, dirCode: String, name: String)

I'd like to see

case class ServiceArea(areaCode: String, directorate: Directorate, name: String)

Is this possible with Slick? Because when I try something like

object ServiceAreas ...
  def * = areaCode ~ directorate ~ name <> (ServiceArea, ServiceArea.unapply _)

it fails to compile, not finding an implicit value for evidence parameter of type TypeMapper[ForeignKeyQuery[ServiceAreas,ServiceArea]].

Community
  • 1
  • 1
Mike
  • 323
  • 3
  • 13
  • 1
    Is [this](http://stackoverflow.com/questions/15147362) my answer? "Slick is not an ORM"? – Mike May 06 '13 at 08:45

1 Answers1

7

You have to adjust your way of thinking a little. Slick handles composition and execution of queries, but as you mentioned in your comment, it's not an ORM (thank goodness).

Have a look at the code you referenced:

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 <> (ServiceArea, ServiceArea.unapply _)
}

The link to directorate is already defined by a navigatable foreign key.

So now you can write a query like this:

   def directorates = for {
    area <- Parameters[String]
    sa <- ServiceAreas if sa.areaCode === area
    dir <- sa.directorate
  } yield dir

Which will yield a Directorate for each match.

Jack
  • 16,506
  • 19
  • 100
  • 167