Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add field in existing table phoenix

So I'm a newbie in phoenix. I created a simple api in my phoenix project the problem is that i want to add another field in my todo.ex

I want to add an author field in my todo.ex

FROM

defmodule TodoApi.Todo do
  use TodoApi.Web, :model

  schema "todos" do
    field :description, :string

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:description])
    |> validate_required([:description])
  end
end

TO

defmodule TodoApi.Todo do
  use TodoApi.Web, :model

  schema "todos" do
    field :description, :string
    field :author, :string

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:description, :author])
    |> validate_required([:description, :author])
  end
end

But I'm getting an postgrex error 42703 column t0.author does not exist

Thanks in advance..

like image 860
Code.Freeze Avatar asked Jan 29 '18 05:01

Code.Freeze


1 Answers

You need to add a migration for todos table.

If you use ecto, just generate a migration using mix ecto.gen.migration todos_add_author_column and add a column in the newly generated priv/repo/migrations/<timestamp>_todos_add_author_column.exs file like this :

def change do
  alter table("todos") do
    add :author, :text
  end
end

Here's a link to docs

like image 119
Ooba Elda Avatar answered Sep 21 '22 13:09

Ooba Elda