Toggle NOT NULL constraint with Rails migration

Take advantage of change_column_null to add or remove NOT NULL constraint on a column. The null flag indicates whether the value can be NULL.

To add the constraint (says column cannot be NULL):

change_column_null :users, :name, false

To remvoe the constraint (allows column to be NULL):

change_column_null :users, :name, true

The method accepts an optional fourth argument to replace existing +NULL+s with some other value. Use that one when enabling the constraint if needed, since otherwise those rows would not be valid.

Note: Please note the fourth argument does not set a column’s default.

What is this Site?

viralpatel.blog is started by me, Viral Patel, as a place to write posts and blogs about problems that I personally face during my day to day and share new learnings about software development work. Even though there is abundant of information out there via various personal blogs or blogs written by technical companies, day in and day out, We as software developers face different problems. By starting out this blog, I wish to explore different problem solving patterns and software design practices that will make easier to build quality and useful software.