Adding `:null => false` column to a table containing records

In PostgreSQL, even without Rails migrations (pure SQL or use PgAdmin
III), if you try to add a column with NOT NULL and set default to
FALSE, you still get the same error. The workaround is to:
1. add the column without NOT NULL.
2. if necessary, UPDATE all rows to FALSE
3. alter the column to go NOT NULL

PostgreSQL in Rails 1.1.6 used to set the not null constraint before the default.

In Rails 1.2 and later, add_column performs these steps:

  1. alter table add column
  2. unless default is null:
    3. change column default
    4. if not null, update table set column=default
  3. if not null, alter table set not null

This should resolve the original poster’s issue if using PostgreSQL.