I'm working with Slick 3 and Play! 2.4 and I have a very common problem that I don't manage to resolve.
I have a table playlist
that can be linked to some tracks with the help of a relation table playlistsTracks
. I want to be able to get all the playlists with their tracks relation and their tracks. My problem is that I don't manage to get the playlists if they do not have any relations.
Here are the 3 tables:
class Playlists(tag: Tag) extends Table[Playlist](tag, "playlists") {
def id = column[Long]("playlistid", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = (id.?, name) <> ((Playlist.apply _).tupled, Playlist.unapply)
}
class PlaylistsTracks(tag: Tag) extends Table[PlaylistTrack](tag, "playliststracks") {
def playlistId = column[Long]("playlistid")
def trackId = column[UUID]("trackid")
def trackRank = column[Double]("trackrank")
def * = (playlistId, trackId, trackRank) <> ((PlaylistTrack.apply _).tupled, PlaylistTrack.unapply)
def aFK = foreignKey("playlistId", playlistId, playlists)(_.id, onDelete = ForeignKeyAction.Cascade)
def bFK = foreignKey("trackId", trackId, tracks)(_.uuid, onDelete = ForeignKeyAction.Cascade)
}
class Tracks(tag: Tag) extends Table[Track](tag, "tracks") {
def uuid = column[UUID]("trackid", O.PrimaryKey)
def title = column[String]("title")
def * = (uuid, title) <> ((Track.apply _).tupled, Track.unapply)
}
For the moment the snippet of code that get the playlists look like this:
val query = for {
playlist <- playlists
playlistTrack <- playlistsTracks if playlistTrack.playlistId === playlist.id
track <- tracks if playlistTrack.trackId === track.uuid
} yield (playlist, playlistTrack, track)
db.run(query.result) map { println }
It prints something like Vector(Playlist, PlaylistTrack, Track)
(what I want) but I solely get the playlists having relations instead of getting all the playlists, even the one without relations as I would like.
I tried a lot of things like using join (or joinFull, joinLeft, joinRight...) but without success, and it is unfortunately difficult to find some example projects with not only very easy relations.