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

David Robertson picture David Robertson · Feb 15, 2012 · Viewed 50.2k times · Source

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. I've googled around but I can't find how to write a migration which adds not null to an existing column.

TIA.

Answer

nates picture nates · Nov 28, 2013

You can also use change_column_null:

change_column_null :table_name, :column_name, false