Ruby on Rails: How do I add a not null constraint to an existing column using a migration?

In my Rails (3.2) app, I have a bunch of tables in my database but I forgot to add a few not null constraints. How can I write a migration which adds not null to an existing column?


Solution 1:

You can also use change_column_null:

change_column_null :table_name, :column_name, false

Solution 2:

For Rails 4+, nates' answer (using change_column_null) is better.

Pre-Rails 4, try change_column.