Add NOT NULL constraint on column in Postgress

Add NOT NULL constraint on column in Postgress

You can't just add a new non-nullable column to a table when it already has entries in it, as those would be pre filled with NULL.

An easy solution is to create it with DEFAULT NULL, update the data and then make it non-nullable.

This can look like this:

ALTER TABLE company ADD enabled_features JSON DEFAULT NULL;
UPDATE company SET enabled_features = '[]';
ALTER TABLE company ALTER COLUMN enabled_features SET NOT NULL;