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
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
①
$ 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)
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!
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.