Nested subqueries with Ecto

I have a User that has one Library which has many Subjects which has many Books.

Given the user_id I wish to select the user’s books.

First attempt

%Library{subjects: subjects} = get_library_by_user_id(user_id) |> Repo.preload([:subjects])
subjects = subjects |> sub -> end)
Books |> where([bk], bk.subject_id in ^subjects) |> Repo.all

Second Attempt (cleaner but now I have to wrangle the Books out of the User struct)

Repo.all(from lib in Library, where: lib.user_id == ^user_id, preload: [{:subjects, :books}])

Third attempt (doesn’t work):

Repo.all(from bk in Book, where: bk.subject_id in subquery(
  from subj in Subject, where: subj.library_id == subquery(from lib in Library, where: user_id == ^user_id, select:,

This is how I’d write it in pure SQL.

Does anybody know either A: How to get the last query working? B: A better way of doing this?


Based on the description I’d probably just do something like:

query =
  from bk in Book,
    join: subj in Subject, on: == bk.subject_id,
    join: lib in Library, on: == subj.library and lib.user_id == ^user_id,
    select: bk

No need for subqueries on something like this yes?


That works. Awesome! Thank you.

1 Like