leaf

leaf

I have 3 tables.

This is just simple representation of relations.

 User (many to many :roles  join through :user_roles)
 Role (many to many :users join through :user_roles)
 Role(belongs_to :prof)

I want to filter out users where prof meets specific condition.

But I am confused about how to join these three . The confusion is how i combine the user and role table.
Thanks.

Showing Posts 1 to 3

hauleth

hauleth

This will not fully solve the @leaf problem, as it in many-to-many relation when you want to filter out specific relation it will not be so easy. Simplest solution would be if Ecto would support existential queries directly which would greatly simplify this query. Instead we need to write something like:

filtered_user_ids =
  from role in Roles,
    inner_join: ur in "user_roles", on: role.id == ur.role_id,
    group_by: ur.user_id,
    having: ^requested_prof_id not in fragment("array_agg(?)", role.prof_id),
    select: %{user_id: ur.user_id}

from user in User,
  inner_join: ur in subquery(^filtered_user_ids), on: user.id == ur.user_id

I haven’t tested that query though, so it can have some issues.


If you want to use existential queries anyway then you can do:

from user in User,
  where: fragment("""
    NOT EXIST (
      SELECT 1
      FROM user_roles
      INNER JOIN roles
        ON roles.id = user_roles.role_id
      WHERE roles.prof_id = ?
        AND user_roles.user_id = ?
    """,
    ^requested_prof_id,
    user.id
  )
leaf

leaf OP

This seems to be working for me:

    |> join(
    :right,
    [u],
    user_roles in UserRoles, on: user_roles.user_id == u.id
  )
  |> join(
    :right,
    [u, user_roles],
    roles in Role, on: user_roles.role_id == role.id
  )
  |> join(
    :right,
    [u, user_roles, roles],
    prof in Prof, on: role.prof_id == prof.id and prof.id in ^prof_ids
— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews