创建迁移文件时分配默认值 [英] Assigning default value while creating migration file
问题描述
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.
推荐答案
默认迁移生成器不处理默认值 (列修饰符,但不包括 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 =>假,:默认=>;0
...并阅读 Rails API
这篇关于创建迁移文件时分配默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!