multiple joins with slick

15,703

Solution 1

The first join results in a Query returning Tuples. One of the tuple components has the foreign key you want to use for the second join. You need to get this component in the second join condition before getting its field. If Companies is the table, that has the field suppId it would look like this:

(for {
  ((computer, company),suppliers) <- Computers leftJoin Companies on (_.companyId === _.id) leftJoin Suppliers on (_._2.suppId === _.id)
  if computer.name.toLowerCase like filter.toLowerCase()
} yield ... )

Solution 2

I think you want this:

for {
    (computer, company) <- Computers leftJoin Companies on (_.companyId === _.id)
    (supp, _) <- company innerJoin Suppliers on (_.suppId === _.id)
    if computer.name.toLowerCase like filter.toLowerCase()
} yield (computer, company, supp)

Of course, I am making assumptions about your model. Not sure if suppliers are linked with companies or computers.

Share:
15,703

Related videos on Youtube

dsr301
Author by

dsr301

Updated on September 05, 2020

Comments

  • dsr301
    dsr301 over 3 years

    For joining between two tables is done like

        (for {
        (computer, company) <- Computers leftJoin Companies on (_.companyId === _.id)
        if computer.name.toLowerCase like filter.toLowerCase()
        }
    

    But in case if joining required between more tables what is the right way trying below but doesnt work

       (for {
        (computer, company,suppliers) <- Computers leftJoin Companies on (_.companyId ===        _.id)
         //not right leftjoin Suppliers on (_.suppId === _.id)
        if computer.name.toLowerCase like filter.toLowerCase()
      }
    
    • dsr301
      dsr301 over 10 years
      computer is associated with supplier
  • dsr301
    dsr301 over 10 years
    here leftJoin Suppliers on (._2.suppId === _.id) is this join on the first tuple? in case if want to join between computer and suppliers i am trying with leftJoin Suppliers on (._1.suppId === _.id) but this one fails
  • dsr301
    dsr301 over 10 years
    This works, but i see that the full query with joins should be in one line , else its showing compilation errors
  • cvogt
    cvogt over 10 years
    changed the answer to be in one line
  • Eric Pabst
    Eric Pabst about 10 years
    I like that the second join is a separate line in the "for" comprehension.
  • Setheron
    Setheron about 10 years
    how does company do the inner join here ? It a model type? (second line in for loop)
  • pedrofurla
    pedrofurla about 10 years
    company is the one defined in the previous line.
  • AlixB
    AlixB over 9 years
    Because you can't reuse company as a Query, it is a Projection. I tested this solution 3 times on Slick 2.1 :p
  • pedrofurla
    pedrofurla over 9 years
    Look at the date of question and date of the answer. Anyways, if I get time I will amend the question.