I'm having trouble with Elixir/Phoenix documentation, so I thought I'd ask here.
I created a table called boards
with some text fields (:title, :owner, etc). I now want to create a migration that will add a new field to it, that will contain multiple records of a new members
table.
What is the correct syntax for the migration file? How do I edit the boards.ex
? Is there a particular location new files need to be, i.e. does the members
definition need to be in boards.ex
?
Here is the code that got it working:
# \lib\vision\boards\board.ex
defmodule Vision.Boards.Board do
use Ecto.Schema
import Ecto.Changeset
schema "boards" do
field :owner, :string
field :team_name, :string
field :title, :string
has_many :members, Vision.Members.Member
timestamps()
end
@doc false
def changeset(board, attrs) do
board
|> cast(attrs, [:title, :owner, :team_name])
|> validate_required([:title, :owner, :team_name])
end
end
# \lib\vision\members\member.ex
defmodule Vision.Members.Member do
use Ecto.Schema
import Ecto.Changeset
schema "members" do
field :role, :string
field :username, :string
belongs_to :board, Vision.Boards.Board
timestamps()
end
@doc false
def changeset(member, attrs) do
member
|> cast(attrs, [:username, :role])
|> validate_required([:username, :role])
end
end
And then in a migration:
# \priv\repo\migrations\*_member_belongs_to_board.exs
defmodule Vision.Repo.Migrations.MemberBelongsToBoard do
use Ecto.Migration
def change do
alter table(:members) do
add :board_id, references (:boards)
end
end
end