Ecto make not null column nullable

Making a column nullable when it was not null is not obvious with Ecto however it’s pretty simple.

Let’s say your first migration was the following :

defmodule Foo.Repo.Migrations.CreateEvents do
  use Ecto.Migration

  def change do
    create table(:events) do
      add(:title, :string, null: false) # Title column cannot be null
    end
  end
end

At some point you want the title to be nullable then you should use the ecto’s modify/3 function.

defmodule Foo.Repo.Migrations.MakeEventTitleNullable do
  use Ecto.Migration

  def change do
    alter table(:events) do
      modify(:title, :string, null: true, from: :string) # Title column is now nullable
    end
  end
end