MySql: DEFAULT does NOT prevent NULL

I just came across a serious bug/gotcha when using mysql.
Integer, default 0 -> set to NULL –> NULL and not 0

Remember
always set NOT_NULL and DEFAULT!

Example

change_column :order_items, :shipping, :boolean, :default=>false

#shipping is nil or false or true
OrderItem.find(:all,:conditions=>{:shipping=>false}) => [] 

change_column :order_items, :shipping, :boolean, :default=>false, :null=>false

OrderItem.find(:all,:conditions=>{:shipping=>false}) => [all]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s