[RUBY] [Rails] Steps to fix problems caused by deleting my sql table and migration file

[Premise] Rails 6.0.3.1 ruby 2.6.3

Currently producing an original app. At that time, I made it by mistake. I summarized the error contents that occurred when trying to delete the table called users_artists and the migration file.

Terminal.


% rails db:rollback STEP=5

-Execute rollback in the terminal to delete the migration file.

Terminal.


% rails db:migrate:status

・ For confirmation, when I checked the status with the above command, I found a problem in only one place.

Terminal.


 Status   Migration ID    Migration Name
--------------------------------------------------
   up     000             ********** NO FILE **********
  down    20201014111144  Devise create users
  down    20201014111251  Devise create artists
  down    20201014121517  Create events
  down    20201020102544  Create bookmarks

↑ The status is up in only one place, and the file name remains as NO FILE for some reason. Image from Gyazo ↑ The above is the contents of the schema file that remained at that time -The version display disappeared and remained without going down. Originally, this file name should have a Migration ID Is "000" here

[Error resolution procedure] (1) Create a migration file under db / migrate with the new name "000_create_users_artists.rb" in order to lower the status. : point_right: In order to make it the same as the name of other migration files, ** create ** is added to the beginning of the name and described as follows.

000_create_users_artists.rb


class CreateUsersArtists < ActiveRecord::Migration[6.0]
  def change
    create_table :users_artists do |t|
      t.integer :user_id
      t.integer :artist_id

      t.timestamps
    end
  end
end

② Execute rails db: migrate: status again. As shown below, it was confirmed that the Migration Name of the up part has changed to `Create users artists` on the terminal.

Terminal.


Status   Migration ID    Migration Name
--------------------------------------------------
   up     000             Create users artists
  down    20201014111144  Devise create users
  down    20201014111251  Devise create artists
  down    20201014121517  Create events
  down    20201020102544  Create bookmarks

(3) Again, specify the file name with the following command to bring down the status and execute it.

Terminal.


% rails db:migrate:down VERSION=000_create_users_artists.rb

④ Again, check if the status of the specified file is down and delete the unnecessary `` `000_create_users_artists.rb``` file OK! !!

Terminal.


 Status   Migration ID    Migration Name
--------------------------------------------------
  down    000             Create users artists
  down    20201014111144  Devise create users
  down    20201014111251  Devise create artists
  down    20201014121517  Create events
  down    20201020102544  Create bookmarks

⑤ Finally, execute rails db: migrate to recreate the table and finish.

Recommended Posts

[Rails] Steps to fix problems caused by deleting my sql table and migration file
Migration file to add comment to Rails table
Rails "How to delete NO FILE migration files"