leaf

leaf OP

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.

First 3 of 3 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement