Adding and Removing a column

Ecto for Beginners : part 4 of 22 published on Sep 06, 2015

Lets add and remove a column from our posts table.

To do this, we will need another migration.

$ mix ecto.gen.migration alter_posts
* creating priv/repo/migrations
* creating priv/repo/migrations/TIMESTAMP_alter_posts.exs

Inside our migration we agoing to change (alter) our table.

# /priv/repo/migrations/TIMESTAMP_alter_posts.exs
defmodule Readdit.Repo.Migrations.AlterPosts do
  use Ecto.Migration

  def change do
    alter table(:posts) do
      add :flagged, :boolean
      remove :advertisement_message
    end
  end
end

And to actually make the change, we of course need to run our migration.

$ mix ecto.migrate
== Running Readdit.Repo.Migrations.AlterPosts.change/0 forward
alter table posts
== Migrated in 0.0s
Next: Drop a table