Home > Enterprise >  How to change a table name?
How to change a table name?

Time:10-08

I am new to Rails. Facing issue while changing table name.

I tried changing the name by going to create_table migration file and changed name from there but it didn't work.

CodePudding user response:

First you need to generate migration for renaming

$ rails g migration RenameOldTableToNewTable

Then inside the newly created migration file you should add rename_table statement

class RenameOldTableToNewTable < ActiveRecord::Migration[6.1]
  def change
    rename_table :old_table_name, :new_table_name
  end 
end

Finally run migrations

$ rails db:migrate

Ref: https://stackoverflow.com/a/471425/13841038

CodePudding user response:

You can create a new migration file for changing the table name and use rename_table command like this:

rename_table :old_name, :new_name
  • Related