Skip to content

Instantly share code, notes, and snippets.

@csokun
Created October 13, 2023 05:04
Show Gist options
  • Save csokun/267962e35e914221d9c88bdcc1f65139 to your computer and use it in GitHub Desktop.
Save csokun/267962e35e914221d9c88bdcc1f65139 to your computer and use it in GitHub Desktop.
Ecto Query - aggr many-to-many count child
# user <1---*> playlist <1---*> playlist_song
(from p in Playlist,
join: u in assoc(p, :user),
left_join: ps in assoc(p, :songs),
where: p.id == ^1 and u.id == ^1,
group_by: p.id,
select: %{
playlist_id: p.id,
song_count: count(ps.id)
}
) |> Repo.one()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment