Migrations - add_column :default=>true, :null=>false

This seems pretty brain dead:

add_column :types, :notify_on_create, :boolean, {:default=>true,
:null=>false}

$ rake migrate

Error: ERROR: column “notify_on_create” contains null values
: ALTER TABLE types ALTER notify_on_create SET NOT NULL

Why doesn’t Rails set the new column to true, as it’s supposed to
default to? I tried :default=>1 as well (same results).

Is there a way to make this migration work?

Joe

Joe R. MUDCRAP-CE wrote the following on 15.10.2006 22:12 :

Why doesn’t Rails set the new column to true, as it’s supposed to
default to? I tried :default=>1 as well (same results).

Is there a way to make this migration work?

Joe

Do you use PostgreSQL by any chance?

Try to load this in your environment.rb:

add_column in the original PostgreSQLAdapter

begins by creating the column and then adds constraints

which simply doesn’t work… AbstractAdapter is ok -> super works

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def add_column(table_name, column_name, type, options = {})
super
end
end

Lionel

Here’s a workaround:

def self.up
# add_column :types, :notify_on_create, :boolean, {:default=>true,
:null=>false}

add_column :types, :notify_on_create, :boolean, :default=>true

# Type.reset_column_information # doesn't seem necessary?
Type.update_all 'notify_on_create=true'
change_column :types, :notify_on_create, :boolean, :null=>false # 

doesn’t change anything
end

As you can see from my comments, reset_column_information doesn’t seem
necessary (contrary to the docs). And the last change_column doesn’t
result in the notify_on_create column getting changed to not null.
Sigh…time to submit some tickets I guess.

Joe

This appears to be fixed in HEAD (way back in July).

Anybody know how to execute this statement in script/console? I can’t
figure out how.

alter table types alter notify_on_create set not null

Thanks,
Joe