Assigning default value while creating migration file

kxhitiz picture kxhitiz · May 29, 2011 · Viewed 99.3k times · Source
rails generate migration AddRetweetsCountToTweet retweets_count:integer 

Ok I use above line to create migration file that automatically generates code in the generated file to add a column to a model Tweet with datatype integer. Now I want to add default value to the added column while generating the migration file. Is that possible? I googled it but couldn't find. Guys need help.

Answer

taro picture taro · May 29, 2011

Default migration generator does not handle default values (column modifiers are supported but do not include default or null), but you could create your own generator.

You can also manually update the migration file prior to running rake db:migrate by adding the options to add_column:

add_column :tweet, :retweets_count, :integer, :null => false, :default => 0

... and read Rails API