ogirginc
ogirginc

Reputation: 5270

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!

Upvotes: 0

Views: 1212

Answers (1)

TTD
TTD

Reputation: 310

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.

Upvotes: 1

Related Questions