Ruby-on-rails 更改列的 Rails 迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2799774/
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
Rails migration for change column
提问by papdel
We have script/generate migration add_fieldname_to_tablename fieldname:datatypesyntax for adding new columns to a model.
我们有script/generate migration add_fieldname_to_tablename fieldname:datatype向模型添加新列的语法。
On the same line, do we have a script/generate for changing the datatype of a column? Or should I write SQL directly into my vanilla migration?
在同一行,我们是否有用于更改列数据类型的脚本/生成?或者我应该将 SQL 直接写入我的 vanilla 迁移中吗?
I want to change a column from datetimeto date.
我想将一列从 更改datetime为date。
回答by Alex Korban
I think this should work.
我认为这应该有效。
change_column :table_name, :column_name, :date
回答by John
You can also use a block if you have multiple columns to change within a table.
如果您在一个表中有多个要更改的列,您也可以使用块。
Example:
例子:
change_table :table_name do |t|
t.change :column_name, :column_type, {options}
end
See the API documentation on the Table classfor more details.
有关更多详细信息,请参阅有关 Table 类的API 文档。
回答by Ryan
I'm not aware if you can create a migration from the command line to do all this, but you can create a new migration, then edit the migration to perform this taks.
我不知道您是否可以从命令行创建迁移来完成所有这些,但是您可以创建一个新的迁移,然后编辑迁移以执行此任务。
If tablename is the name of your table, fieldname is the name of your field and you want to change from a datetime to date, you can write a migration to do this.
如果 tablename 是您的表的名称,fieldname 是您的字段的名称,并且您希望将日期时间更改为日期,则可以编写迁移来执行此操作。
You can create a new migration with:
您可以使用以下命令创建新迁移:
rails g migration change_data_type_for_fieldname
Then edit the migration to use change_table:
然后编辑迁移以使用 change_table:
class ChangeDataTypeForFieldname < ActiveRecord::Migration
def self.up
change_table :tablename do |t|
t.change :fieldname, :date
end
end
def self.down
change_table :tablename do |t|
t.change :fieldname, :datetime
end
end
end
Then run the migration:
然后运行迁移:
rake db:migrate
回答by Aboozar Rajabi
As I found by the previous answers, three steps are needed to change the type of a column:
正如我在前面的答案中发现的那样,更改列的类型需要三个步骤:
Step 1:
第1步:
Generate a new migration file using this code:
使用以下代码生成一个新的迁移文件:
rails g migration sample_name_change_column_type
Step 2:
第2步:
Go to /db/migratefolder and edit the migration file you made. There are two different solutions.
转到/db/migrate文件夹并编辑您制作的迁移文件。有两种不同的解决方案。
def change change_column(:table_name, :column_name, :new_type) end
def change change_column(:table_name, :column_name, :new_type) end
2.
2.
def up
change_column :table_name, :column_name, :new_type
end
def down
change_column :table_name, :column_name, :old_type
end
Step 3:
第 3 步:
Don't forget to do this command:
不要忘记执行以下命令:
rake db:migrate
I have tested this solution for Rails 4 and it works well.
我已经为 Rails 4 测试了这个解决方案,它运行良好。
回答by Mr. Tao
With Rails 5
带导轨 5
From Rails Guides:
从Rails 指南:
If you wish for a migration to do something that Active Record doesn't know how to reverse, you can use
reversible:
如果你希望迁移做一些 Active Record 不知道如何逆转的事情,你可以使用
reversible:
class ChangeTablenameFieldname < ActiveRecord::Migration[5.1]
def change
reversible do |dir|
change_table :tablename do |t|
dir.up { t.change :fieldname, :date }
dir.down { t.change :fieldname, :datetime }
end
end
end
end
回答by Vivek Sharma
Just generate migration:
只需生成迁移:
rails g migration change_column_to_new_from_table_name
Update migration like this:
像这样更新迁移:
class ClassName < ActiveRecord::Migration
change_table :table_name do |table|
table.change :column_name, :data_type
end
end
and finally
最后
rake db:migrate
回答by prasanthrubyist
Another way to change data type using migration
使用迁移更改数据类型的另一种方法
step1:You need to remove the faulted data type field name using migration
step1:您需要使用迁移删除错误的数据类型字段名称
ex:
前任:
rails g migration RemoveFieldNameFromTableName field_name:data_type
Here don't forget to specify data type for your field
这里不要忘记为您的字段指定数据类型
Step 2:Now you can add field with correct data type
第 2 步:现在您可以添加具有正确数据类型的字段
ex:
前任:
rails g migration AddFieldNameToTableName field_name:data_type
That's it, now your table will added with correct data type field, Happy ruby coding!!
就是这样,现在您的表将添加正确的数据类型字段,Happy ruby coding!!
回答by Sebastian Scholl
This is all assuming that the datatype of the column has an implicit conversion for any existing data. I've run into several situations where the existing data, let's say a Stringcan be implicitly converted into the new datatype, let's say Date.
这一切都假设列的数据类型对任何现有数据都有隐式转换。我遇到过几种情况,现有数据,假设 aString可以隐式转换为新数据类型,假设Date.
In this situation, it's helpful to know you can create migrations with data conversions. Personally, I like putting these in my model file, and then removing them after all database schemas have been migrated and are stable.
在这种情况下,了解您可以使用数据转换创建迁移会很有帮助。就我个人而言,我喜欢将它们放在我的模型文件中,然后在所有数据库架构迁移并稳定后删除它们。
/app/models/table.rb
...
def string_to_date
update(new_date_field: date_field.to_date)
end
def date_to_string
update(old_date_field: date_field.to_s)
end
...
def up
# Add column to store converted data
add_column :table_name, :new_date_field, :date
# Update the all resources
Table.all.each(&:string_to_date)
# Remove old column
remove_column :table_name, :date_field
# Rename new column
rename_column :table_name, :new_date_field, :date_field
end
# Reversed steps does allow for migration rollback
def down
add_column :table_name, :old_date_field, :string
Table.all.each(&:date_to_string)
remove_column :table_name, :date_field
rename_column :table_name, :old_date_field, :date_field
end
回答by Gregdebrick
To complete answers in case of editing default value:
要在编辑默认值的情况下完成答案:
In your rails console :
在您的 rails 控制台中:
rails g migration MigrationName
In the migration :
在迁移中:
def change
change_column :tables, :field_name, :field_type, default: value
end
Will look like :
看起来像:
def change
change_column :members, :approved, :boolean, default: true
end

