在创建迁移文件时分配默认值 [英] Assigning default value while creating migration file
问题描述
rails generate migration AddRetweetsCountToTweet retweets_count:integer
好吧,我用上一行创建了迁移文件,该文件会自动在生成的文件中生成代码,以将列添加到数据类型为整数的模型Tweet中.现在,我想在生成迁移文件时向添加的列中添加默认值.那可能吗?我用谷歌搜索,但找不到.伙计们需要帮助.
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.
推荐答案
默认迁移生成器不处理默认值(列修饰符受支持,但不包含default
或null
),但是您可以创建自己的生成器.
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.
您还可以在运行rake db:migrate
之前通过将选项添加到add_column
来手动更新迁移文件:
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
...并阅读 Rails API
这篇关于在创建迁移文件时分配默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!