max4all
How should I build my schema to allow a user to follow another one, like Twitter?
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I considered using Cloak and Cloak Ecto for encryption-at-rest but I noticed that both seem like abandonware these days. Moreover there a...
New
Other Trending Topics
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
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hubertlepicki
I would create something as:
“users”: id, email …
“followings”: id, user_id, follower_id
unique constraing on user_id and follower_id. Both
user_idandfollower_idare referencinguserstable. Just that the one is user being followed, the other one is user who is following that user.StevenXL
@hubertlepicki answer is exactly right.
In a more general sense, what is being suggested is a join table. A join table is used when we need to express has and belongs to many relationships.
In your specific case, the join table is expressing the notion that a user has many followers, and can also belong to many followers.
mkunikow
This is good usage for some graph db like https://neo4j.com/
Import from a relational database into Neo4j - Getting Started
andre1sk
I would say it highly depends on particular use case, while in theory graph dbs are supposed to be faster for working with graphs in reality they are often not and you have to maintain an extra dependency while losing the ability to do transaction for part of your data and a ton of other inconveniences.
OvermindDL1
(How do you reply to a post as a new topic?)
I’ve always written a join table like followings as:
With no base ‘id’ column as I’ve found them useless on join tables, I then make
user_idandfollower_ida combined primary key with indexes onuser_idorfollower_iddepending on which direction (or both) I expect to be accessing it from. Is there actually a good reason to put an ID column on a pure many-to-many join table I am curious?douglascorrea
How do you link the posts with the user and make the “timeline” query?
In twitter, I’m able to see all posts of everybody I’m following. So, if I use an inter-table as mentioned then I need to join it with the Posts table. That join will probably not perform very well if I’m following too many users since the key cardinality will be big (because I will use the composite key (user_id + following_id) joined with “owner_id” from posts table.
Do you think is there any other solution for displaying the timeline? (timeline = posts from people I follow). I know some hierarchical solution in NoSQL databases, but I never implemented that in Relational ones.
andre1sk
at small scale it might be OK
at larger scale you might be caching the “followings” table
and posts in something like Redis or replicating everything to ElasticSearch
and running queries against it.
hubertlepicki
yes in theory you don’t need it. In practice you often want to store some extra fields in the table, simple example is timestamps
inserted_at. I don’t think Ecto will create/update those unless it’s a full model behind it, so, having anidand model is necessity (but please do double check my words here, I assume this is how it works but never actually tried it without).2nd thing is that if you are following the REST principles in your API urls, you can have actions such as
DELETE /followings/:idif you have ID of the resource this way.max4all
@hubertlepicki @StevenXL @OvermindDL1 thank you for your replies.
This is how I built the relationships.
schemas
schema “followings” do
belongs_to :user, User
belongs_to :follower, Follower
schema “users” do
has_many :followings, Following
migration
create table(:followings) do
add :user_id, references(:users, on_delete: :nothing)
add :follower_id, references(:users, on_delete: :nothing)
indexes
create index(:followings, [:user_id])
create index(:followings, [:follower_id])
create index(:followings, [:user_id, :follower_id], unique: true)
peerreynders
Just a nitpick I’m sure,
To me personally this reads as if the people (followers) following the user are being modeled, not the people that the user is following. (Is that the intent?)
Now apparently the term “followee” is actually a thing but it’s not universally liked. The suggested term “subject” seems more appropriate and less controversial for identifying those who are being followed.
An alternate term for join table is association table (or associative entity) - good to know for googling additional information. In fact José Valim has a blog entry “Working with Ecto associations and embeds” discussing the technical aspects of using associations in Ecto.