[Ruby on Rails] Add / Remove Columns

Add column

Execute the command with rails g migration add_ column name you want to add_to_ table name of the addition destination.

For example, if you want to add a column for user_id to the posts table, it will be as follows.

$ rails g migration add_user_id_to_posts

Modify the contents of the migration file (db/migrate) created by this. Since the change method has already been defined, write add_column: table name to add to,: column name to add,: its data type in it.

def change 
  add_column :posts, :user_id, :integer
end

Then run rails db: migrate. Only when you run it will the table change.

$ rails db:migrate

You have successfully added a column.

Delete column

Execute the command with rails g migration remove_ column name you want to remove_to_ table name to remove column.

For example, if you want to delete the user_id column in the posts table, it will be as follows.

$ rails g migration remove_user_id_to_posts

Run rails db: migrate after modifying the contents of the migration file as you would add a column.

def change 
  remove_column :posts, :user_id, :integer
end
$ rails db:migrate

You have successfully deleted the column.

If you want to empty the database once at the initial stage of development after changing the column, execute the following command.

$ rake db:migrate:reset

The database will be empty.

Recommended Posts

[Ruby on Rails] Add / Remove Columns
How to add / remove Ruby on Rails columns
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
[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
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
[Rails] Add / Remove Forms (cocoon)
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Method summary to update multiple columns [Ruby on Rails]
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
[Ruby on Rails] Introduced paging function
Progate Ruby on Rails5 Looking Back
[Ruby on Rails] Add a column with a foreign key constraint
How to use Ruby on Rails
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
I want to add a browsing function with ruby on rails
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command
[Ruby on Rails] About bundler (for beginners)
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Ruby on Rails in Visual Studio Codespaces
[Ruby on Rails] Follow function implementation: Bidirectional
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec