How to add / remove Ruby on Rails columns

Purpose

It's okay to make a table, but is there a way to add columns? I was worried, so I wrote it.

How to add columns

Type the command in the terminal.

rails g migration Add Column name To Table name Column name: Type

For example, if you want to increase only one

 rails g migration AddNameToPosts name:string

If more than one

 rails g migration AddColumnsToPosts name:string day:data

You can use any name for Columns in AddColumnsToPosts.

Finally,

 rails db:migrate

Is OK!

How to delete a column

I will also write how to delete the column.

rails g migration Remove Column name From table name Column name: Type

For example, if you want to reduce only one

 rails g migration RemoveNameFromPosts name:string

If more than one

 rails g migration RemoveColumnsFromPosts name:string day:data

Again, any name is OK for Columns in RemoveColumnsFromPosts.

Finally,

 rails db:migrate

Is OK!

reference

https://qiita.com/ITmanbow/items/2bf4dfa4d9ca705b97df It was very helpful!

Recommended Posts

How to add / remove Ruby on Rails columns
[Ruby on Rails] Add / Remove Columns
How to use Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
How to deploy jQuery on Rails
How to add columns to a table
How to deploy Bootstrap on Rails
[Rails] How to add new pages
Rails on Tiles (how to write)
How to display a graph in Ruby on Rails (LazyHighChart)
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] Button to return to top
I want to add a browsing function with ruby on rails
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby on Rails] How to avoid creating unnecessary routes for devise
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
How to write Rails
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Ruby on Rails Elementary
How to uninstall Rails
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
Ruby on Rails basics
How to conditionally add html.erb class in Rails
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Rails MySQL] How to reset DB on heroku
How to solve the local environment construction of Ruby on Rails (MAC)!
Ruby On Rails Association
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails] How to make the link destination part of the specified id
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
How to implement image posting function using Active Storage in Ruby on Rails
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to install Ruby on an EC2 instance on AWS
[Introduction] Try to create a Ruby on Rails application
Rails / Ruby: How to get HTML text for Mail
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] Change URL id to column name
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby on Rails] From MySQL construction to database change
[rails] How to post images
Portfolio creation Ruby on Rails
How to add ActionText function
Ruby on rails learning record -2020.10.04
[Rails] How to use enum
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
How to use Ruby return