[rails] After option useful when you want to change the order of DB columns

Overview

When I added a new column to the table and checked it using Sequel Pro etc., I had the experience that the newly added column was displayed at the end and it was hard to see, so I tried to find out how to fix it.

Example

Image from Gyazo

If you add a column without specifying anything, the newly added column (total_price, is_cancel) will be placed after updated_at as shown in this figure.

after option

migration.rb


class AddColumnToOrders < ActiveRecord::Migration[6.0]
  def change
    add_column :orders, :total_price, :integer, null: false, after: :user_id
    add_column :orders, :is_cancel, :boolean, null: false, default: 0, after: :total_price 
  end
end

In this way, you can use the after option to specify which column you want the newly added column to follow!

Image from Gyazo I was able to place a new column next to the user_id column.

reference

Control the order of columns added by rails migration

Recommended Posts

[rails] After option useful when you want to change the order of DB columns
When you want to change the MySQL password of docker-compose
A memo when you want to clear the time part of the calendar
When you want to use the method outside
[Rails] How to change the column name of the table
[java tool] A useful tool when you want to send the ipmsg log of PC-A to the specified PC on a regular basis.
[Rails] [bootstrap] I want to change the font size responsively
I want to change the log output settings of UtilLoggingJdbcLogger
[Rails] When the layout change of devise is not reflected
[Ruby] I want to reverse the order of the hash table
When you want to change the wording to be displayed when making a select box from enum
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Comparison of version strings (Java implementation) when you want to branch the process between two versions
[PostgreSQL] If you want to delete the Rails app, delete the database first!
Explanation of the order of rails routes
When you want to ZIP download the image data saved locally
If you want to change the Java development environment from Eclipse
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] How to change the page title of the browser for each page
When you want to check whether the contents of a property can be converted to a specific type
When you have introduced devise but want to add more columns
I want to change the value of Attribute in Selenium of Ruby
[Rails 5] How to display the password change screen when using devise
When you want to add a string type column with a limited length with the `rails generate migration` command
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
[Rails] About the error when displaying the screen due to the autofocus of the form
When you want Rails to disable a session for a specific controller only
I want to know the JSP of the open portlet when developing Liferay
Summary of means when you want to communicate with HTTP on Android
Ruby on Rails When you don't know the cause of rollback when saving.
I want you to use Enum # name () for the Key of SharedPreference
[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
Basic Rails commands you want to learn
[Rails] Change the label name of f.label
The process of introducing Vuetify to Rails
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
If you want to satisfy the test coverage of private methods in JUnit
Defense Techniques When You Have to Fight the Performance of Unfamiliar Applications (Part 2)
What to do when rails db: seed does not reflect in the database
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
If you are using Android Room and want to change the column definition
[Order method] Set the order of data in Rails
I want to output the day of the week
[Rails] Button to return to the top of the page
If you want to modify database columns etc.
When you want to bind InputStream in JDBI3
I want to var_dump the contents of the intent
Change the timezone of the https-portal container to JST
Summary of copy and paste commands used when you want to delete the cache in iOS application development anyway
I want you to put the story that the error was solved when you stabbed the charger in the corner of your head
I want to truncate after the decimal point
[Ruby] When you want to replace multiple characters
Rails The concept of view componentization of Rails that I want to convey to those who want to quit
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
I want to change the path after new registration after logging in with multiple devises.
Library not loaded when trying to upgrade the version of ruby and rails s
[Rails] I want to display the link destination of link_to in a separate tab