문제

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?

Thanks in advance

도움이 되었습니까?

해결책

Run

rails g migration add_default_value_to_table

then in the migration file

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

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

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

다른 팁

To rollback a particular migration:-

rake db:migrate:down VERSION=20080906120000

Then edit the migration and add the default value to that field and run the migration again as

rake db:migrate:up VERSION=20080906120000
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top