Search code examples
scalaleft-joinsqueryl

Multiple Left Joins in squeryl


My data model consists of users. A user can have multiple folders and a Folder can contain multiple accounts.

I want to run a query which returns all of the above information at once.

The query to return the Users with the Folders looks like this:

val usersWithFolder = from(MySchema.users, MySchema.folders)((u, f) =>
                         where(u.idField === f.userId) select ((u, f)))

And I want a query like this:

join(MySchema.users, MySchema.folders.leftOuter, MySchema.accounts.leftOuter)((u, f, a) =>
                         select(u, f, a)
                         on (u.idField === f.map(_.userId), ???))

How do I map the accounts to the folder? I can not use f.idField === a.map(_.folderId) because it looks like f is a List.

Thanks in advance

Flo


Solution

  • I would think that f would be an Option, so this something like this should work:

    join(MySchema.users, 
      MySchema.folders.leftOuter, 
      MySchema.accounts.leftOuter)((u, f, a) =>
        select(u, f, a)
        on (u.idField === f.map(_.userId), 
          f.map(_.idField) === a.map(_.folderId))
      )