Home > Software engineering >  Is it safe to use default value with not null on a new column?
Is it safe to use default value with not null on a new column?

Time:05-13

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!

CodePudding user response:

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.

  • Related