sid3r

sid3r

Querying deeply nested associations

Hello, I’m new to Phoenix and Ecto and having hard time making my own queries, hope you can help me with this one.

My situation is pretty common: I’ve a User which have Relationship(s)

tdefmodule Livechat.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  alias Livechat.Accounts.User
  alias Livechat.Relationship
  alias Livechat.Chat.Message

  schema "users" do
    field :email, :string
    field :password, :string, virtual: true, redact: true
    field :hashed_password, :string, redact: true
    field :confirmed_at, :naive_datetime

    has_many :sent_messages, Message, foreign_key: :from_id
    has_many :received_messages, Message, foreign_key: :to_id

    many_to_many :relationships, User,
      join_through: Relationship,
      join_keys: [user_id: :id, relation_id: :id]

    timestamps()
  end
defmodule Livechat.Relationship do

  use Ecto.Schema
  import Ecto.Changeset

  use Ecto.Schema

  schema "relationships" do
    field :user_id, :id
    field :relation_id, :id
    timestamps()
  end

end

Users send each other Message(s)

defmodule Livechat.Chat.Message do
  use Ecto.Schema
  import Ecto.Changeset
  alias Livechat.Accounts.User

  schema "messages" do
    field :from_id, :integer
    field :to_id, :integer
    field :message, :string
    field :status, :string
    
    belongs_to :from, User, define_field: false
    belongs_to :to, User, define_field: false

    field :messages_count, :integer, virtual: true

    timestamps()
  end
end

I try to retrieve a list of relationships of a given user and the count of unread messages from those relationship to this user

I know how to retrieve the user and its relationships with all sent messages, but don’t know how to build a complex query.

this return the relationships with all sent messages, including messages sent to other users lol, This is not the way to do it, of that I’m sure

Repo.get(User, id)
    |> Repo.preload([relationships: :sent_messages])
    |> Map.get(:relationships, [])

Any idea how to approach this?
thnk you.

Marked As Solved

benstepp

benstepp

Ecto.Query is what you’re going to want to use when building complex queries. The following should work for your use case.

import Ecto.Query

# from a %User{}
query =
  from r in Ecto.assoc(user, :relationships),
    left_join: m in Message,
    on: m.from_id == r.id and m.to_id == ^user.id and m.status == "unread",
    group_by: r.id,
    select: %{user: r, message_count: count(m.id)}


# from a user_id
query =
  from u in User,
    inner_join: r in assoc(u, :relationships),
    left_join: m in Message,
    on: m.from_id == r.id and m.to_id == u.id and m.status == "unread",
    where: u.id == ^user_id,
    group_by: r.id,
    select: %{user: r, message_count: count(m.id)}


Repo.all(query)
[
  %{
    message_count: 7,
    user: #Livechat.Accounts.User<
      __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
      id: 62,
      email: "friend1@livechat.test",
      confirmed_at: nil,
      sent_messages: #Ecto.Association.NotLoaded<association :sent_messages is not loaded>,
      received_messages: #Ecto.Association.NotLoaded<association :received_messages is not loaded>,
      relationships: #Ecto.Association.NotLoaded<association :relationships is not loaded>,
      inserted_at: ~N[2023-06-29 14:28:28],
      updated_at: ~N[2023-06-29 14:28:28],
      ...
    >
  },
  #...
]

As a side note: depending on how often the query gets called, it may be better to use a “counter cache” for performance. There are some postgres trigger based solutions I’ve enjoyed using in the past.

Also Liked

al2o3cr

al2o3cr

Two big questions:

  • what shape do you want the output to be? I see a messages_count virtual field on Message, but I’m not sure what it’s for.

  • can you produce the desired result in plain SQL? It can be helpful to work in the opposite direction (SQL → Ecto) for complicated queries.

A bonus question: how often will your app be computing this data? If the unread counts are accessed much more often than messages are actually marked read, then consider caching the “unread count” on the relationships schema.

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement