How can I rename a database column in a Ruby on Ra

2019-01-03 00:24发布

I wrongly named a column hased_password instead of hashed_password.

How do I update the database schema, using migration to rename this column?

25条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-03 01:16

You have two ways to do this:

  1. In this type it automatically runs the reverse code of it, when rollback.

    def change
      rename_column :table_name, :old_column_name, :new_column_name
    end
    
  2. To this type, it runs the up method when rake db:migrate and runs the down method when rake db:rollback:

    def self.up
      rename_column :table_name, :old_column_name, :new_column_name
    end
    
    def self.down
      rename_column :table_name,:new_column_name,:old_column_name
    end
    
查看更多
登录 后发表回答