How can I rename a database column in a Ruby on Rails migration?

Viewed 586104

I wrongly named a column hased_password instead of hashed_password.

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

31 Answers
rename_column :table, :old_column, :new_column

You'll probably want to create a separate migration to do this. (Rename FixColumnName as you will.):

bin/rails generate migration FixColumnName
# creates  db/migrate/xxxxxxxxxx_fix_column_name.rb

Then edit the migration to do your will:

# db/migrate/xxxxxxxxxx_fix_column_name.rb
class FixColumnName < ActiveRecord::Migration
  def self.up
    rename_column :table_name, :old_column, :new_column
  end

  def self.down
    # rename back if you need or do something else or do nothing
  end
end

For Rails 3.1 use:

While, the up and down methods still apply, Rails 3.1 receives a change method that "knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method".

See "Active Record Migrations" for more information.

rails g migration FixColumnName

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end

If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again:

rename_column :table_name, :old_column1, :new_column1
rename_column :table_name, :old_column2, :new_column2
...

You could use change_table to keep things a little neater:

class FixColumnNames < ActiveRecord::Migration
  def change
    change_table :table_name do |t|
      t.rename :old_column1, :new_column1
      t.rename :old_column2, :new_column2
      ...
    end
  end
end

Then just db:migrate as usual or however you go about your business.


For Rails 4:

While creating a Migration for renaming a column, Rails 4 generates a change method instead of up and down as mentioned in the above section. The generated change method is:

$ > rails g migration ChangeColumnName

which will create a migration file similar to:

class ChangeColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end

In my opinion, in this case, it's better to use rake db:rollback, then edit your migration and again run rake db:migrate.

However, if you have data in the column you don't want to lose, then use rename_column.

See the "Available Transformations" section in the "Active Record Migrations" documentation.

rename_column(table_name, column_name, new_column_name):

Renames a column but keeps the type and content.

I had this challenge when working on a Rails 6 application with a PostgreSQL database.

Here's how I fixed it:

In my case the table_name was "Products", the old_column was "SKU" and the new_column was "ProductNumber".

  1. Create a migration file that will contain the command for renaming the column:

     rails generate migration RenameSKUToProductNumberInProducts
    
  2. Open the migration file in the db/migrate directory:

     db/migrate/20201028082344_rename_sku_to_product_number_in_products.rb
    
  3. Add the command for renaming the column:

     class RenameSkuToProductNumberInProducts < ActiveRecord::Migration[6.0]
       def change
         # rename_column :table_name, :old_column, :new_column
         rename_column :products, :sku, :product_number
       end
     end
    
  4. Save, and then run the migration command:

     rails db:migrate
    

You can now confirm the renaming of the column by taking a look at the schema file:

    db/schema.rb

If you are not satisfied with the renaming of the column, you can always rollback:

    rails db:rollback

Note: Endeavour to modify the column name to the new name in all the places where it is called.

If your code is not shared with other one, then best option is to do just rake db:rollback then edit your column name in migration and rake db:migrate. Thats it

And you can write another migration to rename the column

 def change
    rename_column :table_name, :old_name, :new_name
  end

Thats it.

Let's KISS. All it takes is three simple steps. The following works for Rails 5.2.

1 . Create a Migration

  • rails g migration RenameNameToFullNameInStudents

  • rails g RenameOldFieldToNewFieldInTableName - that way it is perfectly clear to maintainers of the code base later on. (use a plural for the table name).

2. Edit the migration

# I prefer to explicitly write theupanddownmethods.

# ./db/migrate/20190114045137_rename_name_to_full_name_in_students.rb

class RenameNameToFullNameInStudents < ActiveRecord::Migration[5.2]
  def up
    # rename_column :table_name, :old_column, :new_column
    rename_column :students, :name, :full_name
  end

  def down
            # Note that the columns are reversed
    rename_column :students, :full_name, :name
  end
end

3. Run your migrations

rake db:migrate

And you are off to the races!

In the console:

rails generate migration newMigration

In the newMigration file:

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end

I'm on rails 5.2, and trying to rename a column on a devise User.

the rename_column bit worked for me, but the singular :table_name threw a "User table not found" error. Plural worked for me.

rails g RenameAgentinUser

Then change migration file to this:

rename_column :users, :agent?, :agent

Where :agent? is the old column name.

You can write a migration run the below command to update the column name:

rename_column :your_table_name, :hased_password, :hashed_password

Also, make sure that you update any usage of the old column name in your code with the new one.

Just generate the migration using:

rails g migration rename_hased_password

After that edit the migration and add the following line in the change method:

rename_column :table, :hased_password, :hashed_password

This should do the trick.

rails g migration migrationName

So you go to your generated migration and add:

rename_column :table, :old_column, :new_column

to the method

First you need to run

rails g migration create_new_column_in_tablename new_column:datatype
rails g migration remove_column_in_tablename old_column:datatype

and then you need to check db/migration you can check the details in the nem migration, if all the details is correct you need to run:

rails db:migrate
Related