Search code examples
ruby-on-railsdatabasemigrationconstraintsnotnull

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

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

    Pre-Rails 4, try change_column.