About the error that occurred when adding the column name in rails (rails db: migrate, rails db: rollback, add)

table of contents

  1. Overview
  2. Cause
  3. Solution
  4. Reflection
  5. Summary

1. Overview

There are two tables, the Items table and the Users table, and in order to add the user-id column to the Items table created first, I ran rails db: rollback twice and then ran rails db: migrate. However, I got an error that the foreign key does not exist and the user SQL does not exist. (Originally, I should have added a column using add.)

2. Cause

Before creating the Users table, the Items table was created provisionally, so an error occurred that the user-id associated with the Item table does not exist.

For example, suppose you created the first Items table ** as of September 1st **.

class CreateItems < ActiveRecord::Migration[6.0]
  def change
    create_table :items do |t|
      t.references :user,         foreign_key: true
This is related to the cause of the error
      t.string :name,             null: false
      t.timestamps
    end
  end
Omitted below

Suppose you created the second Users table ** as of September 5 **.

class DeviseCreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :name,                        null: false
      t.string :email,                       null: false, default: ""
      t.string :encrypted_password,          null: false, default: ""
The following is omitted

I think the database file will look like db / migrate / 20200905052006_devise_create_users.rb, but the number part "20200905052006" indicates the date and time when the database was created.

Since rails db: migrate will migrate the database one by one in order from the oldest date and time, in this case the database of the Items table created on September 1 will be migrated first. ..

In other words, if you try to migrate the Items table while the Users table has not been migrated, t.references: user, foreign_key: true will not be determined to be valid and there will be no foreign key for the user. I get an error message saying that there is no SQL.

3. Solution

If you rename the number in the date and time part, save the file so that the time series is reversed, and then do rails db: migrate, the Users table will be migrated first and the foreign key does not exist, the user The error that SQL does not exist is resolved.

4. Reflection

  1. I couldn't post an accurate error message because I didn't keep a good record of the error. I will be conscious of posting and take notes with a sense of tension.

  2. In the first place, if the column was added using add, the time series would be incorrect and no error would occur. If it was set to add, it should have been possible to migrate only the usser-id column of the Items table after migrating the User table. It was a mistake to do rails db: rollback twice and change the column names in the Items table directly.

5. Summary

  1. It is better to use add to add columns. Especially when adding a foreign key, rails db: migrate may cause an error.
  2. The number part of the database file indicates the date and time of creation.
  3. rails db: migrate is done in order from oldest date and time. (Rails db: rollback cancels the last one migration.)
  4. If you add a column without using add and get an error that the foreign key does not exist, change the date and time of the database file name for your convenience.

Recommended Posts

About the error that occurred when adding the column name in rails (rails db: migrate, rails db: rollback, add)
Error in rails db: migrate
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
[Rails] About the error that the image is not displayed in the production environment
[Rails] "private method` String' called ~ "error when db: migrate
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment
What to do when an error occurs in rails db: migrate ((StandardError: An error has occurred, this and all later migrations canceled :))
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do when an error (StandardError: An error has occurred, this and all later migrations canceled:) appears in rails db: migrate
PG :: DatatypeMismatch error when doing heroku run rails db: migrate
[Docker] The story that an error occurred in docker-compose up
[Rails] Error that occurred in automatic deployment by Capistrano (fatal: not a valid object name: master)
The story that I struggled because I couldn't do "Rails db: migrate".
About the symbol <%%> in Rails erb
[Rails] Modify migrate file (rails db: rollback)
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
[Rails] I learned about migration files! (Adding a column to the table)
[Rails] How to log in with a name by adding a devise name column
[Rails] About the error when displaying the screen due to the autofocus of the form
How to reference a column when overriding the column name method in ActiveRecord
[Rails] Modify migrate file (rails db: rollback STEP =)
Solution that gives an error when trying to connect to DB (MySQL) in Java
What to do when rails db: seed does not reflect in the database
When I think about the 402 error that suddenly appeared in the middle of the introduction of PAY.jp, there was an unexpected place
Error in bundle install when running rails new
When you get lost in the class name
[Rails] Solution when migration error occurs in acts-as-taggable-on
Error memorandum that occurred when creating a CI / CD environment [Rails + CircleCI + Capistrano + AWS]