stevensonmt
This might not be a specific ecto question as much as a general database setup question. I want users to have relationships to one another. Every example I can find for many to many in ecto involves types from different tables (e.g., users and products). I’m just not sure how to set this up.
Right now in the join table follows I have a column for followed and a column for “follower”. The migration is just:
def change do
create table(:follows) do
add(:follower, references(:user), primary_key: true)
add(:followed, references(:user), primary_key: true)
end
end
The User schema includes many_to_many :follows, User, join_through: "follows". Is this the right way to set it up?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
f0rest8
Hi, Ecto has a guide for this now: Self-referencing many to many.
Hope it helps
stevensonmt
That’s perfect! Thank you so much.
benwilson512
Just my $0.02 here, but I would avoid using
many_to_manyhere (I avoid it in general honestly). You are going to want to store additional columns on that join table, and you’ll need an ecto schema to represent it. At a minimum you’ll likely want to know when a user followed another user, and that gets easier if you have an ecto schema for it.You can always do a
has_many :followers, through: [:follows, :follower]to get back the convenient association.stevensonmt
Not sure I understand how that would work. So the User schema would be
? Are Follows a separate context in this setup?
Then you have separate tables for
followsandfollower? I’m sure I’m thinking of that wrong because that seems like a good way for them to be decoupled and potentially out of sync.f0rest8
If you’re trying to do a follower type system à la Twitter, then the Ruby on Rails Tutorial book walks through that setup toward the end.
And like @benwilson512 said, you don’t need the self-referencing setup. Although, it is important to note, you can very easily store additional columns on a join table in a
many_to_many(even self-referencing) and is shown in the Ecto guide(s). Additionally, The Little Ecto Cookbook from Dashbit is a great resource.But, if you do need to self reference, then the Ecto guide I mentioned is a great example (hopefully!).
dimitarvp
In essence, you’d have a separate table – say, called
user_to_user– that has columns likeuser_from,user_to(both IDs and referencing theuserstable), and various other metadata as well. Which is what @benwilson512 suggests; the associations between the users themselves usually require metadata at a later stage and people get bitten by this because they didn’t architect their DB schema to allow it from the start.entone
Maybe a graph database would solve your problem better?
stevensonmt
@entone, haha. I’m not sure my struggles would be helped by trying to learn yet another technology from scratch, lol. The problem is that I’ve never really done database work so I don’t have a background knowledge of relational database structure to lean on and translate to Ecto. Ecto is basically teaching me database stuff.
@f0rest8 , thanks for the tip about RoR tutorial. I actually did the Learn Enough courses a few years ago but had forgotten about it as my life went a different direction. I still had all the code though and was able to piece it together.
Even though I haven’t finished all the CRUD functions what I have now at least works with respect to querying seed data with no follows. (i.e., it returns empty lists instead of the myriad errors I’ve had all day.) So for those who might stumble on this thread later:
benwilson512
This looks perfect, nicely done!
warronbebster
This is really late, but what does the object/2 function in
MyAppWeb.Schemacome from?