Ecto Migration – migration to not allow null values for Boolean fields

Hi Every one
I want to write migration on already created database in Mysql, don’t have to add new field or remove.
i have to just run migration to check that the some boolean fields in my table to not allow to be a NULL.
does the following script work fine??

def change do
  create constraint("user", :user_name_must_be_not_null, check: "user_name!=NULL")
end

Migrations are a way to change the DB schema and that includes adding stricter validation. If you just want to disallow nulls then the following will do just fine:

def change do
  alter table(:user) do
    modify(:user_name, :string, null: false)
  end
end

This is a builtin DB mechanism, no need to introduce CHECK constraints.

1 Like

You could always run the script and check. That would be better for your learning/improvement as an elixir dev.

1 Like