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.


Thank you, your answers have been sent
These will help me create more relevant content that you’re looking for.
Always stay up to date
I do not share data to anyone

I’ll only use the information you provide on this form to get touch about the topics you’ve chosen above. I do not share this data with anyone else.