[Rails] How to change the column name of the table

How to change the column name of the table

I summarized how to change the column name of the table in Rails. The steps are as follows.

① Create migration file ② Edit migration file ③ Reflected in the database

This time, change the column name as follows.

Change before wheather

After change weather

Model name Column name (before change) Column name (after change)
users wheather weather

① Create migration file

First, create a migration file to change the column name.

$ rails generate migration rename_ [column name before change] _column_to_ [model name (plural)]

This time $rails generate migration rename_wheather_column_to_users Describe as.

② Edit migration file

A new file will be created in / db / migrate, so add the change method and describe the column name you want to change there.

File created this time: 20201115004326_rename_wheather_column_to_users.rb

Describe as follows.

/db/migrate/20201115004326_rename_wheather_column_to_users.rb


class RenameWheatherColumnToUsers < ActiveRecord::Migration[6.0]
  def change
    rename_column :Model name, :Column name (before change), :Column name (after change)
  end
end

In this case, it is described as follows.

/db/migrate/20201115004326_rename_wheather_column_to_users.rb


class RenameWheatherColumnToUsers < ActiveRecord::Migration[6.0]
  def change
    rename_column :users, :wheather, :weather
  end
end

③ Reflected in the database

Finally, it is reflected in the database and the column name change is completed. $rails db:migrate

that's all. I just mistakenly wrote weather as weather, and this kind of work happened (laughs). Please be careful not to misspell it ☆

Recommended Posts

[Rails] How to change the column name of the table
[Ruby on Rails] How to change the column name
[Rails] How to create a table, add a column, and change the column type
How to dynamically change the column name acquired by MyBatis
How to change app name in rails
[Rails] Change the label name of f.label
[Rails] How to change the page title of the browser for each page
[Rails] How to get the contents of strong parameters
How to make a unique combination of data in the rails intermediate table
[Rails] Processing after adding a column to the devise table
How to change the setting value of Springboot Hikari CP
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
How to change the contents of the jar file without decompressing
How to change the file name with Xcode (Refactor Rename)
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Rails] How to get the URL of the transition source and redirect
How to set the IP address and host name of CentOS8
How to name variables 7 selections of discomfort
[Rails] How to use the map method
[Rails] How to introduce kaminari with Slim and change the design
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to determine the number of parallels
How to change the timezone on Ubuntu
[Rails] How to omit the display of the character string of the link_to method
Ransack sort_link How to change the color!
How to sort the List of SelectItem
The process of introducing Vuetify to Rails
[chown] How to change the owner of a file or directory
[Rails 5] How to display the password change screen when using devise
[Rails] I learned about migration files! (Adding a column to the table)
How to decorate the radio button of rails6 form_with (helper) with CSS
[Rails] How to log in with a name by adding a devise name column
How to change the maximum and maximum number of POST data in Spark
How to solve the local environment construction of Ruby on Rails (MAC)!
How to change the value of a variable at a breakpoint in intelliJ
[Ruby On Rails] How to search the contents of params using include?
How to make the schema of the URL generated by Rails URL helper https
[swift5] How to change the color of TabBar or the color of item of TabBar with code
How to reference a column when overriding the column name method in ActiveRecord
[Rails] How to solve the time lag of created_at after save method
Change the save destination of the image to S3 in the Rails app. Part 2
[Rails] How to decide the destination by "rails routes"
[Rails] Button to return to the top of the page
[Rails] Introduction of pry-rails ~ How to debug binding.pry
Customize how to divide the contents of Recyclerview
How to get today's day of the week
Change the timezone of the https-portal container to JST
Output of how to use the slice method
How to use JQuery in js.erb of Rails6
How Microservices Change the Way of Developing Applications
How to display the result of form input
http: // localhost: How to change the port number
[Java] How to get the authority of the folder
How to check Rails commands in the terminal
How to write Rails
How to uninstall Rails
graphql-ruby: How to get the name of query or mutation in controller Note
[Rails] How to register multiple records in the intermediate table with many-to-many association
[Rails] Get the path name of the URL before the transition and change the link destination
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed