I used the following article as a reference.
↓
rails db:migrate:reset failed so rails db:reset
sell
Ruby
,
Rails
,
SQLite3
from the conclusion
If an error occurs when migrating the db, attempting to reset the db and migrate at the same time with rails db:migrate:reset may fail.
There may be a problem with the execution order in the migration file, so it may be possible to solve it by performing rails db:reset and rails db:migrate separately.
environment
Rails 5.1.2
Ruby 2.4.3
what happened
Error in migration
When I was proceeding with the rails tutorial and tried to migrate the db, I got this error.
$ rails db:migrate
== 20171229141042 CreateUsers: migrating ======================================
– add_index(:users, :email, {:unique=>true})
→ 0.0010s
– create_table(:users)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table “users” already exists: CREATE TABLE “users” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, “name” varchar, “email” varchar, “created_at” datetime NOT NULL, “updated_at” datetime NOT NULL)
…
I’m getting angry that I’m trying to create a user table when it already exists.
Error even with reset:migration
So I tried rails db:migrate:reset.
(This command is mentioned in chapter 7.4.3 Real user registration)
$ rails db:migrate:reset
Dropped database ‘db/development.sqlite3’
Dropped database ‘db/test.sqlite3’
Created database ‘db/development.sqlite3’
Created database ‘db/test.sqlite3’
== 20171229141042 CreateUsers: migrating ======================================
– add_index(:users, :email, {:unique=>true})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: main.users: CREATE UNIQUE INDEX “index_users_on_email” ON “users” (“email”)
…
Also, I was angry that there was no users table.
Looking at the flow of processing, we are trying to add an index after deleting and recreating the DB. And the process to create the users table has not been done.
take a look at the migration file
Looking at the migration file, I found the cause.
I have added_index before creating the users table.
It seems that it is caused by various operations on the DB before creating the migration file while proceeding with the rails tutorial.
class CreateUsers < ActiveRecord::Migration[5.1]
def change
add_index :users, :email, unique: true
create_table :users do |t|
t. string :name
t. string : email
t.timestamps
end
end
end
What you wrote here helped me.
I could fix the migration like in the article above, but the official says it’s not recommended, and I’ve already touched the data here and there, so I solved the error by following the steps below.
try rails:db:reset
$rails:db:reset
Dropped database ‘db/development.sqlite3’
Dropped database ‘db/test.sqlite3’
Created database ‘db/development.sqlite3’
Created database ‘db/test.sqlite3’
– create_table(“users”, {:force=>:cascade})
→ 0.0036s
– create_table(“users”, {:force=>:cascade})
→ 0.0021s
The DB was deleted and recreated, and the users table was successfully created.
If you migrate after this, it will succeed without problems because the users table exists.
Difference between rails db:reset and rails db:migration:reset
Both rails db:reset and rails db:migration:reset are commands to recreate after deleting all DB, but there are the following differences.
About rails db:reset
rails db:reset creates DB based on db/schema.rb. db/migrate/**.rb is not used.
Rails Guides has the following description:
The bin/rails db:reset task drops and reconfigures the database. This task is equivalent to bin/rails db:drop db:setup.
This task is not equivalent to running all migrations. This is because this task reuses the current contents of schema.rb. If the migration cannot be rolled back, it may not be possible to recover by running bin/rails db:reset. For more information on schema dumps, see the Significance of Schema Dumps section.
4.3 Reset the database
Check the documentation of the rails db:setup command (4.2 Setting up the database) as it is synonymous with rails db:drop db:setup.
Create database
Schema loading
Initializing the database with seed data
It is written that the three processes of are executed.
About rails db:migrate:reset
On the other hand, rails db:migrate:reset seems to execute db/migrate/**.rb from the oldest after deleting the DB.
This seems to be used when you want to apply a new migration after deleting the DB once when you modify the migration file.
By the way, this command was not listed in the latest Rails Guides and was only listed in the Rails 4 series document.
It doesn’t exist even if you search with rails -T.
There is an instruction to execute this command in the English version of the 5 series tutorial, but is it actually treated as a secret command…?
at the end
I think that the method of executing commands separately for reset and migrate should be used in the early stages of development when data is touched here and there.
In order to solve the root cause of the error, it is necessary to check the DB and migration files and correct the inconsistencies.