LEFT JOIN in Slick

168 views
Skip to first unread message

Alexander Abdugafarov

unread,
Apr 9, 2014, 10:01:48 AM4/9/14
to scala...@googlegroups.com
Hi guys.

Is there a standard way to execute a LEFT JOIN with Slick? As far as I understand, stuff like
(user, image) <- Users leftJoin RemoteImages
works only for TableQuery's, not for anything little more complex. Besides, it provides no way to do more than 1 LEFT JOIN.
In my opinion, LEFT JOIN's are one of the most basic feature anyone would expect from Slick, but Google suspiciously yields nothing. So am I missing something there?

Pedro Furlanetto

unread,
Apr 9, 2014, 11:27:19 AM4/9/14
to scala...@googlegroups.com
A pointer to somewhat related attempt in starting a discussion in the
subject: https://groups.google.com/d/msgid/scalaquery/c4ffadf9-ef29-4c21-ae08-1a0f6611f1e3%40googlegroups.com?utm_medium=email&utm_source=footer
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Slick / ScalaQuery" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scalaquery+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/scalaquery/dfcbc795-92ff-4306-8fb2-cc12e9a23929%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Pedro Furlanetto

unread,
Apr 9, 2014, 11:35:18 AM4/9/14
to scala...@googlegroups.com
Btw, here I am doing more than one left join:

val x5 = (for {
((c,e),cc) <- (Company leftJoin Employee on (_.id ===
_.companyId)) leftJoin CompanyClients on { case ((c,e), cc) =>
cc.companyId === c.id }
} yield (c,e, cc))

Pedro Furlanetto

unread,
Apr 9, 2014, 11:39:12 AM4/9/14
to scala...@googlegroups.com
I'd suggest this other approach, but it doesn't work:

val x6 = (for {
(c,e) <- Company leftJoin Employee on (_.id === _.companyId)
(c2, cc) <- c leftJoin CompanyClients on { case (c, cc) =>
cc.companyId === c.id }
} yield (c2, e, cc))

I expected this to work.

Alexander Abdugafarov

unread,
Apr 9, 2014, 11:42:55 AM4/9/14
to scala...@googlegroups.com
Thanks for the tip on multiple joins on TableQuery's! The original question still stands, though.

Pedro Furlanetto

unread,
Apr 9, 2014, 11:48:30 AM4/9/14
to scala...@googlegroups.com
Well, is the original question is "So am I missing something there?"
and after giving an option I must answer: Yes, you are missing plenty
:)
> https://groups.google.com/d/msgid/scalaquery/24d48860-3f1f-4edc-85c0-47c2151e9e43%40googlegroups.com.

Vadim Belobrovka

unread,
Apr 9, 2014, 12:19:31 PM4/9/14
to scala...@googlegroups.com
I think Alexander is looking for a way to COMPOSE 2 (or more) slick queries via LEFT JOIN

I am very interested in that as well and cannot find any elegant solutions or even workarounds ... If anyone has an idea of a way to do this please chime in

Thanks!

Pedro Furlanetto

unread,
Apr 9, 2014, 12:23:42 PM4/9/14
to scala...@googlegroups.com
Vadim,
Didn't I do that? I surely admit it's not elegant.
> https://groups.google.com/d/msgid/scalaquery/0fd999b8-c45c-44cd-9bbd-afd4c5c89e7c%40googlegroups.com.

Alexander Abdugafarov

unread,
Apr 9, 2014, 8:15:29 PM4/9/14
to scala...@googlegroups.com
Pedro,
Indeed you did, but that's composition of TableQuery queries (i.e. queries for the single whole table), not general Slick queries. Still workable, I admit, but that greatly limits query composition and reusability - main point of Lifted Queries API

Pedro Furlanetto

unread,
Apr 10, 2014, 11:18:55 AM4/10/14
to scala...@googlegroups.com
Alexander,

As I understand it, general Slick queries *are* TableQueries. Look the
code below:


val x5 = (for {
((c,e),cc) <- (Company leftJoin Employee on (_.id ===
_.companyId)) leftJoin CompanyClients on { case ((c,e), cc) =>
cc.companyId === c.id }
} yield (c,e, cc))

val restricted = x5.filter{ case (company, employee, cclient) =>
company.name === "ACME" }

As I made simple filter I could have mapped or flatmappted! Thus
composing the query.

Either I am failing to see your point or you are indeed missing something.

On Wed, Apr 9, 2014 at 9:15 PM, Alexander Abdugafarov
> https://groups.google.com/d/msgid/scalaquery/bba09abd-eec8-4e2e-9cc8-8f764c174403%40googlegroups.com.

Pedro Furlanetto

unread,
Apr 10, 2014, 11:20:15 AM4/10/14
to scala...@googlegroups.com
Amending: "I could have mapped or flatmappted" with other TablesQueries.

Naftoli Gugenheim

unread,
Jun 8, 2014, 12:34:59 AM6/8/14
to scala...@googlegroups.com

What issue do you get if you try to leftJoin another type of Query?

Reply all
Reply to author
Forward
0 new messages