What to do when an error (StandardError: An error has occurred, this and all later migrations canceled:) appears in rails db: migrate

Introduction

This will be a memo for learning.

This time, we will deal with the error that occurs after executing $ rails db: migrate or $ rails db: migrate.

== 20200107095832 CreateMicroposts: migrating =================================
-- create_table(:microposts)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "microposts" already exists: CREATE TABLE "microposts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "content" text, "user_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)

Continued below

Cause

I think there is something wrong with the migration of the command I did last time. Probably an error occurred in the middle of the migration process and the table was generated, but the migration that generated the table remains unexecuted. Is it in such a state? I think

solution

$ rails db:migrate:reset

After resetting the database

$ rails db:migrate

Migrate again

This command is used when you want to delete the database, modify the new migration file, and perform migration again.

$ rails db:migrate:reset

An error occurs after resetting the database!

ActiveRecord::NoEnvironmentInSchemaError: 

Environment data not found in the schema. To resolve this issue, run: 

        bin/rails db:environment:set RAILS_ENV=development


Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
(See full trace by running task with --trace)

Coping

rake db:migrate:status

Check the status of db with the above command. ⬇︎ Execution content

 up     20201013130002  Devise create users
 down    20201015132219  Add devise to users

Apparently, the migration file at the bottom is suspicious. .. .. I checked the editor I created the same migration file twice. .. So, let's delete the migration file at the bottom.

$ rm -rf db/migrate/20201015132219_add_devise_to_users.rb

This will migrate again. ⬇︎ Execution result

== 20201013130002 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0061s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0014s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0011s
== 20201013130002 DeviseCreateUsers: migrated (0.0087s) =======================

I was able to do it without any problems!

Finally

This time I learned about errors when migrating. DB reset did not solve the problem. The cause was a duplicate migration file.

I would appreciate it if you could point out any mistakes.

Recommended Posts

What to do when an error (StandardError: An error has occurred, this and all later migrations canceled:) appears in rails db: migrate
What to do when an error occurs in rails db: migrate ((StandardError: An error has occurred, this and all later migrations canceled :))
Error in docker rails db: migrate (StandardError: An error has occurred, all later migrations canceled :)
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
What to do and how to install when an error occurs in DXRuby 1.4.7
[Rails] Error StandardError: An error has occurred, all later migrations canceled: Column `Foreign key name` on table` Table name` does not match column ʻid` on `Table name`
[Programming beginner] What to do when rails s becomes an error in the local development environment
What to do if you get an error during rails db: reset
What to do when Blocked Host: "host name" appears in Ruby on Rails
What to do if you get an error on heroku rake db: migrate
What to do if ffi installation fails when launching an application in Rails
What to do when you launch an application with rails
About the error that occurred when adding the column name in rails (rails db: migrate, rails db: rollback, add)
Error in rails db: migrate
What to do when a could not find driver appears when connecting to a DB in a Docker environment
What to do if you get an error in Basic authentication during Rails test code
What to do if an error occurs when doing npm install axios in React + Typescript project
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
Add gem'rails-i18n','~> 6.0.0' and what to do if bundle install gives an error
What to do if an error occurs when nokogiri enters when bundle install
What to do if you get an error when you hit Heroku logs
What to do when "call'Hoge.connection' to establish a connection" appears on rails c
Solution that gives an error when trying to connect to DB (MySQL) in Java
[Grails] Error occurred running What to do when the Grails CLI does not start
Summary when trying to use Solr in Java and getting an error (Solr 6.x)
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field
[Rails] What to do when you want to generate an error that cannot be destory when foreign key restrictions are applied
What to do if you get an error saying "Could not find a JavaScript runtime." When starting rails server
After all, what is [rails db: migrate] doing?
What to do when IllegalStateException occurs in PlayFramework
[In team development] Error when moving to another member's branch and doing rails s [Rails]
What to do if Failure / Error: require File.expand_path ('../ config / environment', __dir__) appears in RSpec
What to do when Address already in use is displayed after executing rails s
[Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key
<Dot installation> What to do if you cannot proceed due to an error when building a development environment for Rails learning.
I added a Jar file to the build path in an Eclipse project, but the situation and what to do when `java.util.zip.ZipException: invalid LOC header (bad signature)` appears.
What to do when The SSL certificate has expired
[Rails] "private method` String' called ~ "error when db: migrate
What to do when a null byte error occurs
What to do when rails creates a 〇〇 2.rb file
What to do if an ActionController :: UnknownFormat error occurs
Error ExecJS :: RuntimeUnavailable: What to do when it occurs
[Ruby / Rails] What to do when NoMethodError appears when using a destructive method such as filter!
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
[Rails] What to do if you can't get an error message with the errors method
What to do if you get a "302" error in your controller unit test code in Rails
What to do when Rails on Docker does not reflect controller changes in the browser
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]