Rails5+Docker環境におけるActiverecord関連付け後マイグレーションのエラー(2)

###続き


#1)　staffテーブルのマイグレーションが成功しない

```ターミナル 
docker-compose exec web bundle exec rails db:migrate:status
```

上記でmigrationの状況を確認できると。
すると

```
Status   Migration ID    Migration Name
--------------------------------------------------
   up     20200831161252  Create clinics
  down    20200831164400  Create staffs

```

となる。
downとなりstaffsテーブルが、migration実行されてない?ため、指定して実行。

```
docker-compose exec web bundle exec rails db:migrate:up VERSION=20200831164400
```

しかし、以下エラー

```ターミナル 
== 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
```

インデックスはすでにありますよ、ってことだろうけど、
staffsのマイグレーションファイルに、

```
t.references :clinic, foreign_key: true
```

と外部キーを設定しているだけで、他には何も余計なことはしていないきがするんだけど、なぜなんだろう。



#2) ユニークキー制約は効いていない？？

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
```

となっていて、ユニークキー制約は効いてない気がする。

以下スタッフテーブルマイグレーションファイル

```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
    #ユニークキー制約
    add_index　:staffs, :staff_name, unique: true
  end
end
```

以下クリニックテーブルマイグレーションファイル

```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
    #ユニークキー制約
    add_index　:clinics, :clinic_name, unique: true
  end
end

```

ますます謎は深まるばかり、、、

追記(9/9)

```
rails db:migrate:reset
```
とすることでなぜか解決した。