Hi all! First time posting, let me know if I can structure my question better.
I am using Ecto migrations on a project and need to add columns with non-NULL constraints to a table with existing data. My down
simply removes the columns. My up
adds the columns, uses default values to get placeholder data in the DB, flushes, and then uses Ecto.Migration.modify/3
to add the non-NULL constraints and remove the defaults. However, when I look at the table description in Postgres, I see not null default NULL::character varying
. It looks like the database still behaves properly, but shouldn’t this modifier be gone entirely?
Here’s my migration’s up
:
def up do
# Step 1. Add location fields with default values
alter table(:campuses) do
add :street_1, :string, default: "__INVALID_STREET"
add :street_2, :string
add :city, :string, default: "__INVALID_CITY"
add :state, :string, size: 2, default: "ZZ"
add :zip, :string, size: 16, default: "__INVALID_CITY"
add :lon, :float, default: 0.0
add :lat, :float, default: 0.0
end
# Step 2. Execute above commands.
flush()
# Step 3. Remove default option and add null constraint
alter table(:campuses) do
modify :street_1, :string, null: false, default: nil
modify :street_2, :string
modify :city, :string, null: false, default: nil
modify :state, :string, size: 2, null: false, default: nil
modify :zip, :string, size: 16, null: false, default: nil
modify :lon, :float, null: false, default: nil
modify :lat, :float, null: false, default: nil
end
end
Thanks all!