2

I have the following slick entities:

class Person(personId, houseId, carId)
class House(houseId)
class Car(carId)

I want to select a Person and their optional house and car, my query is:

val query = personTable
      .filter(_.personId === personId)
      .joinLeft(houseTable)
      .on(_.houseId === _.houseId)
      .joinLeft(carTable)
      .on(_._1.carId === _.carId)
      .result
      .headOption

However, the return type of the query looks a little funny, I'd expect it to be a tuple(person, house, car):

Option[(Person, Option[House], Option[Car])]

But it's actually a tuple(tuple(person, house), car):

Option[((Person, Option[House]), Option[Car])]

The data that comes back does seem correct, its just in an unusual structure, maybe I'm not performing the multiple joins correctly above?

Rory
  • 798
  • 2
  • 12
  • 37
  • Perhaps if you try to rewrite this within a for-comprehension (look at the example in the [applicative join](http://slick.lightbend.com/doc/3.0.0/queries.html?highlight=join#applicative-joins)), you would be able to change the format of the query to return a single tuple. Since for-comprehensions are just, nice, syntactic sugars, a `map` would probably do the job as well – Yaneeve Oct 11 '18 at 11:35

1 Answers1

5

Things look normal to me.

If you don't like the current datatype, you can map in your query to change it, like this e.g:

val query = personTable
      .filter(_.personId === personId)
      .joinLeft(houseTable)
      .on(_.houseId === _.houseId)
      .joinLeft(carTable)
      .on(_._1.carId === _.carId)
      .map{case((person, houseOpt), carOpt) => (person, houseOpt, carOpt)}
      .result
      .headOption
C4stor
  • 8,355
  • 6
  • 29
  • 47