[Ruby on Rails] How to change the column name

Introduction

environment ruby-2.6.3

During the development process, I had to change the column names. I will write down the coping method as a memorandum.

Thing you want to do

Change the column name of model user as follows

zipCode → zip_code

Method

1. Create a migration file

Execute the following command db/migrate/20200825114039_rename_zip_code_column_to_users.rb Create a.

rails generate migration rename_zipcode_to_users

2. Edit the migration file

db/migrate/20200825114039_rename_zip_code_column_to_users.rb


class RenameZipCodeColumnToUsers < ActiveRecord::Migration[6.0]
  def change
    rename_column :users, :zipCode, :zip_code
  end
end

3. Reflect in the database

Do the following

$ rake db:migrate

The column name of the users model has been changed from zipCode to zip_code! </ b>

At the end

Initially, after rewriting the column name in the code I thought I should do "rake db: migrate", but it wasn't.

It's a little troublesome until you get used to it, but even if you make similar mistakes in the future It seems that we can deal with it calmly.

Recommended Posts

[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
[Rails] How to change the column name of the table
How to use Ruby on Rails
How to dynamically change the column name acquired by MyBatis
How to change app name in rails
[Ruby on Rails] How to use CarrierWave
How to change the timezone on Ubuntu
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby On Rails] How to update the calculated result to an integer type column using update_column
How to solve the local environment construction of Ruby on Rails (MAC)!
[Rails] How to create a table, add a column, and change the column type
How to debug the processing in the Ruby on Rails model only on the console
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails] How to write enum in Japanese
[Ruby On Rails] How to reset DB in Heroku
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) How to create models and tables
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Change the update date and creation date to your favorite notation
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to deploy jQuery on Rails
How to deploy Bootstrap on Rails
Rails on Tiles (how to write)
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
How to display a graph in Ruby on Rails (LazyHighChart)
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
How to run React and Rails on the same server
How to change the file name with Xcode (Refactor Rename)
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Rails] How to use the map method
part of the syntax of ruby ​​on rails
[Ruby on Rails] Change the save destination of gem refile * Note
[Rails] How to introduce kaminari with Slim and change the design
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
Deploy to Heroku [Ruby on Rails] Beginner
[Rails] How to change the page title of the browser for each page
[Rails] Change the label name of f.label
Preparing to introduce jQuery to Ruby on Rails
Ransack sort_link How to change the color!
[Rails] How to put a crown mark on the ranking function
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[For beginners] How to get the Ruby delayed railway line name
[Ruby on Rails] Use the resources method to automatically create routes.
Things to remember and concepts in the Ruby on Rails tutorial
[Ruby on Rails] Button to return to top
[Ruby on Rails] How to avoid creating unnecessary routes for devise
(Ruby on Rails6) Create a function to edit the posted content
[Ruby on Rails] Pass the parameters divided by date_select to FormObject.
[Rails 5] How to display the password change screen when using devise