Ruby-on-rails 创建迁移文件时分配默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6167994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning default value while creating migration file
提问by kxhitiz
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.
好的,我使用上面的行来创建迁移文件,该文件会在生成的文件中自动生成代码,以将一列添加到数据类型为整数的模型推文中。现在我想在生成迁移文件时为添加的列添加默认值。那可能吗?我用谷歌搜索但找不到。伙计们需要帮助。
回答by taro
Default migration generator does not handle default values (column modifiersare supported but do not include defaultor null), but you could create your own generator.
默认迁移生成器不处理默认值(支持列修饰符但不包括default或null),但您可以创建自己的生成器。
You can also manually update the migration file prior to running rake db:migrateby adding the options to add_column:
您还可以在运行之前手动更新迁移文件,方法rake db:migrate是将选项添加到add_column:
add_column :tweet, :retweets_count, :integer, :null => false, :default => 0
add_column :tweet, :retweets_count, :integer, :null => false, :default => 0
... and read Rails API
...并阅读Rails API
回答by Jits
回答by MSC
Yes, I couldn't see how to use 'default' in the migration generator command either but was able to specify a default value for a new string column as follows by amending the generated migration file before applying "rake db:migrate":
是的,我也看不到如何在迁移生成器命令中使用“default”,但能够通过在应用“rake db:migrate”之前修改生成的迁移文件来为新字符串列指定默认值,如下所示:
class AddColumnToWidgets < ActiveRecord::Migration
def change
add_column :widgets, :colour, :string, default: 'red'
end
end
This adds a new column called 'colour' to my 'Widget' model and sets the default 'colour' of new widgets to 'red'.
这会向我的“小部件”模型添加一个名为“颜色”的新列,并将新小部件的默认“颜色”设置为“红色”。
回答by Madhan Ayyasamy
I tried t.boolean :active, :default => 1 in migration file for creating entire table. After ran that migration when i checked in db it made as null. Even though i told default as "1". After that slightly i changed migration file like this then it worked for me for setting default value on create table migration file.
我在迁移文件中尝试了 t.boolean :active, :default => 1 来创建整个表。在我签入数据库时运行该迁移后,它变为空值。即使我告诉默认为“1”。在那之后,我稍微更改了这样的迁移文件,然后它为我设置了创建表迁移文件的默认值。
t.boolean :active, :null => false,:default =>1. Worked for me.
t.boolean :active, :null => false,:default =>1。对我来说有效。
My Rails framework version is 4.0.0
我的 Rails 框架版本是 4.0.0
回答by Artur Pedrosa
You would have to first create your migration for the model basics then you create another migration to modify your previous using the change_column ...
您必须首先为模型基础创建迁移,然后创建另一个迁移以使用 change_column ...
def change
change_column :widgets, :colour, :string, default: 'red'
end

