[2.4] Slick 3.0 multiple left joins possible?

422 views
Skip to first unread message

Bruno Batarelo

unread,
Aug 17, 2015, 2:48:36 PM8/17/15
to play-framework
Hi All,

  from documentation, it's clear how to make a single left join between two tables.

val q = for {(t, v) <- titles joinLeft volumes on (_.uid === _.titleUid)} yield (t, v)

Query q will, as expected, have entities: _1 of type Titles and _2 of type Rep[Option[Volumes]] since volumes can be null in db.

Further cascading is problematic:

val q = for {((t, v), c) <- titles joinLeft volumes on (_.uid === _.titleUid) joinLeft chapters on (_.uid === _.volumeUid)} yield ...

This won't work because _.uid === _.volumeUid is invalid given _.uid not existing.

According to various sources on the net, this shouldn't be an issue, but then again, sources tend to target different slick versions and 3.0 is still rather new. Does anyone have some clue on the issue?
To clarify, idea is to use two left joins to extract data from 3 cascading 1:n:n tables.

Thanks,
Bruno

v6ak

unread,
Aug 31, 2015, 4:53:49 PM8/31/15
to play-framework
I have successfuly used two leftJoins in Slick 3.0.

There are two complications.

One is with joins on multiple tables, where you can't just use syntax (_.uid === _.volumeUid), but you have tuples of tables. You'll have to write something like on {case ((t, v), c) => v.uid === v.titleUid}.

The second one is specific to left joins and is not present in general joins. It is caused by the semantical difference between plain joins and left joins. In case of left join, the corresponding record in the joined table might not exist. Plain SQL uses NULL values for that case. Slick tends not to use NULL values, but it uses Option type instead. You have to somehow handle the case of None value.

Regards,
Vít Šesták 'v6ak'
Reply all
Reply to author
Forward
0 new messages