'Is it safe to use default value with not null on a new column?

We have a Rails app powered by Postgresql v11.4 where I want to add a new column with a default value and a not null constraint like below:

add_column :blog, :published, :boolean, default: false, null: false

Ankane's Strong Migration gem says; adding a new column with a default value is safe and does not require a table rewrite.

Is still safe when combined with a null constraint? Thanks!



Solution 1:[1]

Yes, if there are no existing data the null constraint is safe but if you want to add a column and populate it with existing data, and the existing data could be null your data migration will fail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TTD