stevensonmt

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?

Showing Posts 1 to 10

f0rest8

f0rest8

Hi, Ecto has a guide for this now: Self-referencing many to many.

Hope it helps :heart:

stevensonmt

stevensonmt OP

That’s perfect! Thank you so much.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Just my $0.02 here, but I would avoid using many_to_many here (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

stevensonmt OP

Not sure I understand how that would work. So the User schema would be

schema "users" do 
...
has_many :followers, through: [:follows, :follower]
has_many :follows

? Are Follows a separate context in this setup?
Then you have separate tables for follows and follower? 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

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!).

:heart:

dimitarvp

dimitarvp

In essence, you’d have a separate table – say, called user_to_user – that has columns like user_from, user_to (both IDs and referencing the users table), 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

entone

Maybe a graph database would solve your problem better?

stevensonmt

stevensonmt OP

@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:

defmodule MyApp.Relationship do 
def get_followers(arg) do
    Relationship
    |> where([r], ^arg.user_id == r.followed)
    |> Repo.all()
  end

def get_followers(arg) do
    Relationship
    |> where([r], ^arg.user_id == r.followed)
    |> Repo.all()
  end

  def datasource() do
    Dataloader.Ecto.new(Repo, query: &query/2)
  end

  def query(queryable, _) do
    queryable
  end
end
...
defmodule MyApp.Relationships.Relationship do 
@attrs [:following, :followed]

  schema "relationships" do
    belongs_to(:following, User)
    belongs_to(:followed, User)
    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, @attrs)
  end
end
...
defmodule MyApp.Accounts.User do 
  schema "users" do
  ...
    has_many :active_relationships, Relationship, foreign_key: :following_id
    has_many :passive_relationships, Relationship, foreign_key: :followed_id
    has_many :followers, through: [:passive_relationships, :followed]
    has_many :following, through: [:active_relationships, :following]
  end
end
...
defmodule MyAppWeb.Schema do 
  object :user do 
    field :followers, list_of(:user) do
      resolve(dataloader(Relationship))
    end

    field :following, list_of(:user) do
      resolve(dataloader(Relationship))
    end
...
defmodule MyApp.Repo.Migrations.AddRelationshipsTable do
  use Ecto.Migration

  def change do
    create table(:relationships) do
      add(:followed_id, :id)
      add(:following_id, :id)

      timestamps()
    end

    create(index(:relationships, [:followed_id]))
    create(index(:relationships, [:following_id]))
    create(unique_index(:relationships, [:followed_id, :following_id]))
  end
end
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This looks perfect, nicely done!

warronbebster

warronbebster

This is really late, but what does the object/2 function in MyAppWeb.Schema come from?

Where Next? Top

Trending in Questions Top

RSP87
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
kszambelanczyk
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
RemyXRenard
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
velrest
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
samoloth
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
nseaSeb
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
FlyingNoodle
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 Top

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
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews