What to do if you cannot roll back the migration (UnknownMigrationVersionError)

Introduction

When I tried to roll back the migration file with rails db: rollback, I was suffering from the error UnknownMigrationVersionError as below, so I will write about this solution.

Terminal


rails aborted!
ActiveRecord::UnknownMigrationVersionError: 

No migration with version number 20201230115014.

Details of UnknownMigrationVersionError

When I checked the status of the migration file with rails db: migrate, there was some unfamiliar data written as NO FILE. This was because the migration file was deleted on the VS code with the status up, so the data remained on the DB. (Probably the file I created by forgetting skip-migration when I created the model with ActiveHash \ ...) In summary, the cause of the UnknownMigrationVersionError was that the migration data, which did not have a corresponding file, remained.

D5E70B3A-1D63-479C-94FD-900F409069C4_4_5005_c.jpeg

Solutions

1. Create a migration file on VS code

First, create a file on the VS code that corresponds to the data whose ID is 20201230115014, which was NO FILE. (This time, it is created with the file name "20201230115014.dummy.rb".)

99E65E07-FF88-42CC-9BC3-3C9C084D0334_4_5005_c.jpeg

2. Bring the status down

If you try to roll back with just the migration file created, the following error will occur. This is because the contents of the migration file created earlier are not described.

Terminal


rails aborted!
NameError: uninitialized constant Dummy

To solve this, write the class and change action in the migration file. After that, perform migration once.

ruby:20201230115014.dummy.rb


class Dummy < ActiveRecord::Migration[6.0]
  def change
  end
end

Terminal


rails db:migrate

In this state, if you roll back, the status will be down safely. A5D9F5BE-30C3-4305-87D0-8AE73B397D3D_4_5005_c.jpeg

3. Delete the target file

At this point, all you have to do is delete the target file. After deleting the file "20201230115014.dummy.rb" with VS code, if you check with rails db: migrate: status, you can confirm that the target data can be deleted safely. 5C22EAFC-90C1-4B95-8F5A-AB7B82F34315_4_5005_c.jpeg

At the end

I casually thought that deleting the migration file without checking the status would be troublesome later. I hope it will be a hint for those who see something.

reference

https://qiita.com/ISSO33/items/33a935cb3255c269bef2

Recommended Posts

What to do if you cannot roll back the migration (UnknownMigrationVersionError)
What to do if you cannot execute with the command "Java package name / class name"
What to do if you install Ubuntu
What to do if the server tomcat dies
What to do if you push incorrect information
[Composer] [Laravel] What to do if you cannot install due to insufficient memory
What to do if you don't like the code generated by swagger-codegen-cli
What to do if the debug gem installation fails
What to do if the Rails server can't start
What to do if you get the error message unrecognized selector send to instance "***"
What to do if you accidentally create a model
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
How to translate the error message into Japanese (What to do if you cannot log in for some reason)
What to do if you can't get the text of an element in Selenium
What to do if you get the error Couldn't find Item without an ID
# What to do if you accidentally do rails db: migrate: drop
[Solution] What to do if you get a Docker error'ERROR: Cannot connect to the Docker daemon at unix: ///var/run/docker.sock. Is the docker daemon running?'
[Java] [Spring] What to do if you cannot Autowire with Type Mismatch after annotating Spring Security
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
What to do if you change the Listen Address from the settings screen with Mattermost docker
What to do if you installed Ruby with rbenv but the version does not change
What to do if you get a "Cannot Pull Container Error" when starting ECS ​​Fargate
[Ubuntu 20.04] What to do if the external monitor is not recognized
What to do if validation doesn't work with the update action
What to do if Cloud9 is full in the Rails tutorial
What to do if the Eclipse Maven dependency Jar is wrong
What to do if you get angry with OpenSSL with pyenv install
[Rails] What to do if you can't get parameters with form_with
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you get an error during rails db: reset
What to do if the prefix c is not bound in JSP
What to do if you get an uninitialized constant Likes Controller error
What to do if you get an error when you hit Heroku logs
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
What do you use when converting to String?
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do when you want to delete a migration file that is "NO FILE"
What to do if you get Could not locate Gemfile or .bundle / directory
What to do if the changes are not reflected in the jar manifest file
[Swift] What to do if the app icon is set but not reflected
What to do if you have installed Java for OS X on macOS
What to do if you get an error on heroku rake db: migrate
What to do if you get angry if you don't have nokogiri while installing wp2txt
What to do if SPA URL loading cannot be detected by WebView shouldOverrideUrlLoading
What to do if the rails server doesn't run out on AWS cloud9
What to do if the build fails with the error "Module compiled with Swift x.x.x cannot be imported by the Swift x.x.x compiler"
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you get a javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake in the IBM JDK
What to do if the update does not take effect after deploying Rails AWS
[React.useRef] What to do when the latest state cannot be referenced in the event listener
What to do if you hit the wall of Too many symbol files when uploading with App Store Connect
What to do if you get a wrong number of arguments error in binding.pry
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
What should I do to reload the updated Dockerfile?
What to do if an ActionController :: UnknownFormat error occurs
If you want to recreate the instance in cloud9
What to do if you can't find your API key after deploying to Rails heroku
What to do if the JSONHint annotation does not work with lombok and JSONIC
What to do if you get a port error when docker-compose up on Mac