Migration error after Activerecord association in Rails5 + Docker environment (2)

Continued

1) Staff table migration is not successful

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)

What is an index error ??

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?

2) Is the unique key constraint not working? ??

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

Migration error after Activerecord association in Rails5 + Docker environment (2)
Migration error after Activerecord association in Rails5 + Docker environment
Show Better Errors in Rails + Docker environment
(Basic authentication) environment variables in rails and Docker
[Rails] Solution when migration error occurs in acts-as-taggable-on
500 Internal Server Error occurs in Rails production environment
[Rails] ActiveRecord :: HasManyThrough Order Error in Users # show
Model association in Rails
Rails Docker environment construction
Understand migration in rails
One case of solving a migration error in Rails
SSL in the local environment of Docker / Rails / puma
ActiveRecord :: NotNullViolation in Devise error
Error in rails db: migrate
npm error in docker tutorial
[Docker] Rails 5.2 environment construction with docker
exited with code 1 error resolution with docker-compose up in rails environment
Exit code 1 occurs when Rails is stopped in Docker environment
Rails + MySQL environment construction with Docker
[Rails] Unexpected validation error in devise
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
How to install Pry after building Rails development environment with Docker
Install laravel/Dusk in docker environment (laravel6)
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
Check MySQL logs in Docker environment
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
[Rails] About the error that the image is not displayed in the production environment
[Rails 5] Countermeasures for git commit failure from another branch in Docker environment
Alert slack with alert manager in Docker environment
[Docker] Use environment variables in Nginx conf
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Create Rails 6 + MySQL environment with Docker compose
Migrate existing Rails 6 apps to Docker environment
Use docker in proxy environment on ubuntu 20.04.1
Edit Mysql with commands in Docker environment
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
[Rails] Reset the database in the production environment
[Rails / Heroku] Error resolution procedure after push
How to build Rails 6 environment with Docker
Rails migration
(Capistrano) After deploying, I get a We're sorry… error in the production environment.
[Rails] ActiveRecord
How to deal with the error yaml.scanner.ScannerError: while scanning for the next token that appeared in Rails environment construction with Docker
Docker Desktop for Windows 2.4.0.0 (48506) Startup error after update
Self-hosting with Docker of AuteMuteUs in Windows environment
Just install Laravel8 on docker in PHP8 environment
Error in bundle install when running rails new
[Rails] Run LINEBot in local environment using ngrok
rails tutorial About account activation in production environment
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Rails5.1 + puma SSL connection in local production environment
INTERNAL ERROR: cannot create temporary directory !: Docker environment
Support out of support in docker environment using centos6
Docker × Laravel HTTPS (SSL) communication in local environment
[Rails] How to build an environment with Docker
Introduce dotenv to Docker + Rails to manage environment variables
[Rails] How to use PostgreSQL in Vagrant environment