在创建迁移文件时分配默认值 [英] Assigning default value while creating migration file

查看:59
本文介绍了在创建迁移文件时分配默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

默认迁移生成器不处理默认值(列修饰符受支持,但不包含defaultnull),但是您可以创建自己的生成器.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆