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.
Related videos on Youtube
Author by
dsr301
Updated on September 05, 2020Comments
-
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 over 10 yearscomputer is associated with supplier
-
-
dsr301 over 10 yearshere 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 over 10 yearsThis works, but i see that the full query with joins should be in one line , else its showing compilation errors
-
cvogt over 10 yearschanged the answer to be in one line
-
Eric Pabst about 10 yearsI like that the second join is a separate line in the "for" comprehension.
-
Setheron about 10 yearshow does company do the inner join here ? It a model type? (second line in for loop)
-
pedrofurla about 10 years
company
is the one defined in the previous line. -
AlixB over 9 yearsBecause you can't reuse
company
as a Query, it is a Projection. I tested this solution 3 times on Slick 2.1 :p -
pedrofurla over 9 yearsLook at the date of question and date of the answer. Anyways, if I get time I will amend the question.