docker-compose exec web bundle exec rails db:migrate:status
If you can check the status of migration above. Then
Status Migration ID Migration Name
--------------------------------------------------
up 20200831161252 Create clinics
down 20200831164400 Create staffs
Will be. Since it becomes down and the staffs table is not executed for migration, specify it and execute it.
docker-compose exec web bundle exec rails db:migrate:up VERSION=20200831164400
But the following error
== 20200831164400 CreateStaffs: migrating =====================================
-- create_table(:staffs)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
Index name 'index_staffs_on_clinic_id' on table 'staffs' already exists
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:1144:in `add_index_options'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/mysql/schema_creation.rb:61:in `index_in_create'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_creation.rb:49:in `block in visit_TableDefinition'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_creation.rb:49:in `map'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_creation.rb:49:in `visit_TableDefinition'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_creation.rb:14:in `accept'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:278:in `create_table'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:423:in `create_table'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:846:in `block in method_missing'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:815:in `block in say_with_time'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:815:in `say_with_time'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:835:in `method_missing'
/app/db/migrate/20200831164400_create_staffs.rb:3:in `change'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:789:in `exec_migration'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:773:in `block (2 levels) in migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:772:in `block in migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:771:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:951:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1232:in `block in execute_migration_in_transaction'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1302:in `ddl_transaction'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1231:in `execute_migration_in_transaction'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1190:in `run_without_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1142:in `block in run'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1319:in `with_advisory_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1142:in `run'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1017:in `run'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/railties/databases.rake:99:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/railties-5.0.7.2/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/usr/local/bundle/gems/railties-5.0.7.2/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/usr/local/bundle/gems/railties-5.0.7.2/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/usr/local/bundle/gems/railties-5.0.7.2/lib/rails/commands.rb:18:in `<top (required)>'
/app/bin/rails:9:in `require'
/app/bin/rails:9:in `<top (required)>'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/client.rb:30:in `run'
/usr/local/bundle/gems/spring-2.1.1/bin/spring:49:in `<top (required)>'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>'
/app/bin/spring:15:in `require'
/app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Caused by:
ArgumentError: Index name 'index_staffs_on_clinic_id' on table 'staffs' already exists
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:1144:in `add_index_options'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/mysql/schema_creation.rb:61:in `index_in_create'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_creation.rb:49:in `block in visit_TableDefinition'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_creation.rb:49:in `map'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_creation.rb:49:in `visit_TableDefinition'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_creation.rb:14:in `accept'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:278:in `create_table'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:423:in `create_table'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:846:in `block in method_missing'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:815:in `block in say_with_time'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:815:in `say_with_time'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:835:in `method_missing'
/app/db/migrate/20200831164400_create_staffs.rb:3:in `change'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:789:in `exec_migration'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:773:in `block (2 levels) in migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:772:in `block in migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:771:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:951:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1232:in `block in execute_migration_in_transaction'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1302:in `ddl_transaction'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1231:in `execute_migration_in_transaction'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1190:in `run_without_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1142:in `block in run'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1319:in `with_advisory_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1142:in `run'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1017:in `run'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/railties/databases.rake:99:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/railties-5.0.7.2/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/usr/local/bundle/gems/railties-5.0.7.2/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/usr/local/bundle/gems/railties-5.0.7.2/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/usr/local/bundle/gems/railties-5.0.7.2/lib/rails/commands.rb:18:in `<top (required)>'
/app/bin/rails:9:in `require'
/app/bin/rails:9:in `<top (required)>'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/client.rb:30:in `run'
/usr/local/bundle/gems/spring-2.1.1/bin/spring:49:in `<top (required)>'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load'
/usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>'
/app/bin/spring:15:in `require'
/app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate:up
(See full trace by running task with --trace)
Index name 'index_staffs_on_clinic_id' on table 'staffs' already exists
I'm sure there is an index already, In the staffs migration file,
t.references :clinic, foreign_key: true
I feel like I'm not doing anything else just by setting the foreign key, but why?
Looking at schema.rb
schema.rb
ActiveRecord::Schema.define(version: 20200831161252) do
create_table "clinics", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "clinic_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "staffs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "clinic_id"
t.string "staff_name", null: false
t.string "password_digest", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["clinic_id"], name: "index_staffs_on_clinic_id", using: :btree
end
add_foreign_key "staffs", "clinics"
end
I feel that the unique key constraint is not working.
Below staff table migration file
20200831164400_create_staffs.rb
class CreateStaffs < ActiveRecord::Migration[5.0]
def change
create_table :staffs do |t|
t.references :clinic, foreign_key: true
t.string :staff_name, null: false
t.string :password_digest, null: false
t.timestamps
end
#Unique key constraint
add_index :staffs, :staff_name, unique: true
end
end
Clinic table migration file below
20200831161252_create_clinics.rb
class CreateClinics < ActiveRecord::Migration[5.0]
def change
create_table :clinics do |t|
t.string :clinic_name, null: false
t.timestamps
end
#Unique key constraint
add_index :clinics, :clinic_name, unique: true
end
end
The mystery is getting deeper and deeper ...
Postscript (9/9)
rails db:migrate:reset
I solved it for some reason.
Recommended Posts