Ruby-on-rails 通过迁移向列添加默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7098602/
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
Add a default value to a column through a migration
提问by Jon
How do I add a default value to a column that already exists through a migration?
如何向通过迁移已存在的列添加默认值?
All the documentation I can find shows you how to do it if the column doesn't already exist but in this case it does.
我能找到的所有文档都向您展示了如果该列不存在但在这种情况下它确实存在的方法。
回答by Maurício Linhares
Here's how you should do it:
以下是您应该如何做:
change_column :users, :admin, :boolean, :default => false
But some databases, like PostgreSQL, will not update the field for rows previously created, so make sure you update the field manaully on the migration too.
但是某些数据库(例如 PostgreSQL)不会更新先前创建的行的字段,因此请确保在迁移时也手动更新该字段。
回答by Gazza
change_column_default :employees, :foreign, false
回答by csi
For Rails 4+, use change_column_default
对于Rails 4+,使用change_column_default
def change
change_column_default :table, :column, value
end
回答by bfcoder
Using def changemeans you should write migrations that are reversible. And change_columnis not reversible. You can go up but you cannot go down, since change_columnis irreversible.
使用def change意味着您应该编写可逆的迁移。并且change_column不可逆。你可以上升,但你不能下降,因为这change_column是不可逆转的。
Instead, though it may be a couple extra lines, you should use def upand def down
相反,虽然它可能是几行额外的行,但您应该使用def up和def down
So if you have a column with no default value, then you should do this to add a default value.
所以如果你有一列没有默认值,那么你应该这样做来添加一个默认值。
def up
change_column :users, :admin, :boolean, default: false
end
def down
change_column :users, :admin, :boolean, default: nil
end
Or if you want to change the default value for an existing column.
或者,如果您想更改现有列的默认值。
def up
change_column :users, :admin, :boolean, default: false
end
def down
change_column :users, :admin, :boolean, default: true
end
回答by Praveen George
**Rails 4.X +**
**导轨 4.X +**
As of Rails 4 you can't generate a migration to add a column to a table with a default value, The following steps add a new column to an existing table with default value true or false.
从 Rails 4 开始,您无法生成迁移以将列添加到具有默认值的表中, 以下步骤将新列添加到具有默认值 true 或 false 的现有表中。
1. Run the migration from command line to add the new column
1.从命令行运行迁移以添加新列
$ rails generate migration add_columnname_to_tablename columnname:boolean
The above command will add a new column in your table.
上面的命令将在您的表中添加一个新列。
2. Set the new column value to TRUE/FALSE by editing the new migration file created.
2. 通过编辑创建的新迁移文件将新列值设置为 TRUE/FALSE。
class AddColumnnameToTablename < ActiveRecord::Migration
def change
add_column :table_name, :column_name, :boolean, default: false
end
end
**3. To make the changes into your application database table, run the following command in terminal**
**3. 要对应用程序数据库表进行更改,请在终端中运行以下命令**
$ rake db:migrate
回答by axeltaglia
Execute:
执行:
rails generate migration add_column_to_table column:boolean
It will generate this migration:
它将生成此迁移:
class AddColumnToTable < ActiveRecord::Migration
def change
add_column :table, :column, :boolean
end
end
Set the default value adding :default => 1
设置默认值添加:default => 1
add_column :table, :column, :boolean, :default => 1
add_column :table, :column, :boolean, :default => 1
Run:
跑:
rake db:migrate
耙数据库:迁移
回答by rookieRailer
This is what you can do:
这是你可以做的:
class Profile < ActiveRecord::Base
before_save :set_default_val
def set_default_val
self.send_updates = 'val' unless self.send_updates
end
end
EDIT: ...but apparently this is a Rookie mistake!
编辑:...但显然这是一个新手错误!

