Thank you all for the help so far. On further reflection in terms of what I am trying to do I realized its not a true friendship model. I really just want to keep track of all of the users that have been invited and accepted by say User 1, then do the same for User 2, 3, etc. I dont need to keep track of if User 4 is connected to User 2 who is then connected to User 1. I will likely change the table name from Friendship but for now I am going to leave it and get the functionality I need working first. Its more like keeping track of which users are subscribed to a specific user.
So I have created the following code so far which gets me pretty close I think, however I am getting the error below because I put timestamp columns via my migration but put_assoc doesnt seem to add them. Any help getting the timestamps added and also comments on better ways to accomplish this would be most helpful!
** (Postgrex.Error) ERROR 23502 (not_null_violation) null value in column "inserted_at" violates not-null constraint
User Schema
defmodule App.Users.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
field :name, :string
field :phone_number, :string
field :username, :string
field :email, :string, null: false
many_to_many :friends, User,
join_through: "friendships",
join_keys: [from_user_id: :id, to_user_id: :id]
timestamps()
end
@doc false
def changeset(user_or_changeset, attrs) do
required_fields = [:username, :name, :email, :phone_number]
user_or_changeset
|> cast(attrs, required_fields)
|> validate_required(required_fields)
end
def changeset_friend(changeset, friend_user) do
changeset
|> put_assoc(:friends, [friend_user])
end
end
User Migration
defmodule App.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users) do
add :name, :string, null: false
add :username, :string, null: false
add :email, :string, null: false
add :phone_number, :string, null: false
timestamps()
end
create unique_index(:users, [:username, :email, :phone_number])
end
end
Friendship Schema
defmodule App.Users.Friendship do
use Ecto.Schema
import Ecto.Changeset
schema "friendships" do
timestamps()
end
end
Friendship Migration
defmodule App.Repo.Migrations.AddCreateFriendshipTable do
use Ecto.Migration
def change do
create table(:friendships, primary_key: false) do
add :from_user_id, references(:users)
add :to_user_id, references(:users)
add :accepted, :boolean, default: false
timestamps()
end
end
end
Users Context Module
defmodule App.Users do
import Ecto.Query, warn: false
alias App.Repo
alias App.Users.User
def add_friend(user, friend_user) do
user
|> Repo.preload(:friends)
|> Ecto.Changeset.change()
|> User.changeset_friend(friend_user)
|> Repo.update()
end
end