I’d like to add a field to a table and populate the field as well in the migration. Working with examples always helps. Let’s add username to users table.
def up do
alter table(:users) do
add :username, :string, null: false
end
create unique_index(:users, [:username])
from(u in App.User, update: [set: [username: u.id]])
|> App.Repo.update_all([])
end
def down do
alter table(:users) do
remove :username
end
end
The migration above gaves me: ** (Postgrex.Error) ERROR (undefined_column): column "username" of relation "users" does not exist. Is there a callback or something similar to the up and down functions?
I’m not sure how to do this but you won’t be able to add that column with null: false if you already have users in your table since they will all have a null value.
It seems like flush fixed ** (Postgrex.Error) ERROR (undefined_column): column "username" of relation "users" does not exist.
But as @Linuus mentioned, there are already users in the table so I’m now getting ** (Postgrex.Error) ERROR (not_null_violation): column "username" contains null values.