5

I have a sample query with slick as below:

val query =
  (for {
    (company,loc) <- Company leftJoin Location  on (_.locId === _.id)
    (_,typeof) <- Company leftJoin Types on (_.typeId === _.id)
  } yield (company, loc, typeof))

Is a better way to do multiple joins?

I have tried the suggestions in multiple joins with slick but resulting in errors.

Community
  • 1
  • 1
dsr301
  • 759
  • 3
  • 7
  • 21
  • What do you mean by better way? What is bad with this? – i.am.michiel Sep 04 '13 at 09:06
  • I saw the query generated it is generating multiple query on same table Company two times and joining once with location and once with type. Normally with sql it happens in one query multiple joins. Want to know if there is something wrong with this. – dsr301 Sep 04 '13 at 09:10

3 Answers3

4

This is working fine.

for {
    ((company,geo),typeof) <- Company 
        leftJoin Location on (_.locId === _.id) 
        leftJoin Business_Levels on (_._1.typeId === _.id)
}
i.am.michiel
  • 10,281
  • 7
  • 50
  • 86
dsr301
  • 759
  • 3
  • 7
  • 21
2

You can chain the join normally :

for {
    (company, location, type) <- Company 
        leftJoin Location  on (_.locId === _.id) 
        leftJoin Types on (_._1.typeId === _.id)
} yield (company, location, type)

And by the way, I am quite sure the word type is a scala reserved word.

EDIT : Added the _.1 on line 3 after dsr301's comment.

i.am.michiel
  • 10,281
  • 7
  • 50
  • 86
  • yep, type is keyword, post doesn't have original names for some reasons. any way changed it. It gives me compilation error with what you mentioned above. ')' expected but '.' found. at (_.typeId === _.id) – dsr301 Sep 04 '13 at 09:24
  • After changing the full query to one lone getting this error "typeId is not a member of (models.company.type, models.Location.type)" . I think for the second join is performed on the tuple generated from the first join – dsr301 Sep 04 '13 at 09:31
1

You could check auto joins tricks from scala days presentation about slick - http://www.parleys.com/play/51c2e20de4b0d38b54f46243/chapter50/agenda

Check at 0:27:30

1esha
  • 1,722
  • 11
  • 17
  • and the corresponding code at https://github.com/cvogt/play-slick/tree/master/samples/computer-database/app :) – cvogt Sep 05 '13 at 11:39