hey im trying to add adresses to users in my DB… I have gone through the getting started guide and association guide in hexdocs… but im still waay off… This is what i got so far.
User.ex
defmodule Squarestore.Identity.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
field :fname, :string
field :lname, :string
field :phone, :string
field :email, :string
field :password, :string
has_many :adresses, Squarestore.Identity.Adress
timestamps()
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:fname, :lname, :phone, :email, :password])
|> validate_required([:fname, :lname, :phone, :email, :password])
end
end
adress.ex
defmodule Squarestore.Identity.Adress do
use Ecto.Schema
import Ecto.Changeset
schema "adresses" do
field :adress, :string
field :city, :string
field :country, :string
field :postnr, :string
belongs_to :user, Squarestore.Identity.User
timestamps()
end
@doc false
def changeset(adress, attrs) do
adress
|> cast(attrs, [:adress, :postnr, :country, :city])
|> validate_required([:adress, :postnr, :country, :city])
end
end
allso added an extra migration file called adress_belongs_to_user.exs
defmodule Squarestore.Repo.Migrations.AdressBelongsToUser do
use Ecto.Migration
def change do
alter table(:adresses) do
add :user_id, references(:users)
end
end
end
Hope anyone here can help me understand my problem. =)