迁移以创建默认值 [英] Migration to create default value

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

问题描述

所以 我想知道是否可以编写新的迁移操作,以设置已经创建的字段的默认值.我知道如何编写编辑旧迁移并设置其默认值.我想这样做是因为我已经进行了很多迁移,现在无法回滚并编辑该文件. 而且,是否有任何方法可以回滚到特定的迁移?

So I want to know whether we can write a new migration just to set default value of a field,which is already created. I know how to write edit the old migration and set its default value. I want to do this because I have done so many migrations and I cant roll back now and edit that file. And also, Is there any way to rollback to a particular migration?

预先感谢

推荐答案

运行

rails g migration add_default_value_to_table

然后在迁移文件中

def up
    change_column :table_name, :column_name, :string, :default => "abcd"
end

def down
    change_column :table_name, :column_name, :string, :default => nil
end

我以数据类型string为例,您可以对具有任何数据类型的列进行操作

I've used datatype string as an example, you can do it for column with any datatype

这篇关于迁移以创建默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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