Migration error after Activerecord association in Rails5 + Docker environment

Create and associate a DB with the clinic and the staff working there.

rails 5 series ruby 2.4.5 Docker The following environment

FROM ruby:2.4.5
RUN apt-get update -qq && apt-get install -y build-essential nodejs
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app

Docker-compose.yml


version: '3'
services:
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - 3000:3000
    depends_on:
      - db
    tty: true
    stdin_open: true
  db:
    image: mysql:5.7
    volumes:
      - db-volume:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: hoge
volumes:
  db-volume:

As a result, Activerecord table association is performed.

First, the staff table

docker-compose exec web bundle exec rails g model staff clinic:references staff_name:string password_digest:string

Made as

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

Added foreign key, NN constraint and UQ constraint.

As a clinic table

docker-compose run web bundle exec rails g model clinic clinic_name:string

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

Similarly, I added foreign keys, NN constraints, and UQ constraints.

Finally,

docker-compose run web bundle exec rails db:migrate

After migrating

e-s-mba:test_csv_app eishunsudo$ docker-compose exec web bundle exec rails db:migrate
== 20200831161252 CreateClinics: migrating ====================================
-- create_table(:clinics)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'clinics' already exists: CREATE TABLE `clinics` (`id` int AUTO_INCREMENT PRIMARY KEY, `clinic_name` varchar(255) NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `_query'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `block in query'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:119:in `handle_interrupt'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:119:in `query'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `block in execute'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
/usr/local/bundle/gems/activesupport-5.0.7.2/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `execute'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/mysql/database_statements.rb:31:in `execute'
/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/20200831161252_create_clinics.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:1203:in `block in migrate_without_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1202:in `each'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1202:in `migrate_without_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1150:in `block in migrate'
/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:1150:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1006:in `up'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:984:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/tasks/database_tasks.rb:163:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/railties/databases.rake:58:in `block (2 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:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'clinics' already exists: CREATE TABLE `clinics` (`id` int AUTO_INCREMENT PRIMARY KEY, `clinic_name` varchar(255) NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `_query'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `block in query'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:119:in `handle_interrupt'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:119:in `query'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `block in execute'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
/usr/local/bundle/gems/activesupport-5.0.7.2/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `execute'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/mysql/database_statements.rb:31:in `execute'
/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/20200831161252_create_clinics.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:1203:in `block in migrate_without_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1202:in `each'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1202:in `migrate_without_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1150:in `block in migrate'
/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:1150:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1006:in `up'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:984:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/tasks/database_tasks.rb:163:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/railties/databases.rake:58:in `block (2 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:
Mysql2::Error: Table 'clinics' already exists
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `_query'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `block in query'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:119:in `handle_interrupt'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:119:in `query'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `block in execute'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
/usr/local/bundle/gems/activesupport-5.0.7.2/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `execute'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/mysql/database_statements.rb:31:in `execute'
/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/20200831161252_create_clinics.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:1203:in `block in migrate_without_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1202:in `each'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1202:in `migrate_without_lock'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1150:in `block in migrate'
/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:1150:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:1006:in `up'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/migration.rb:984:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/tasks/database_tasks.rb:163:in `migrate'
/usr/local/bundle/gems/activerecord-5.0.7.2/lib/active_record/railties/databases.rake:58:in `block (2 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
(See full trace by running task with --trace)
e-s-mba:test_csv_app eishunsudo$

It became. Is this a successful migration? Error statement when rails db: migrate Index name ○○ on table Solution for existing table name Before this, I got an Index ~ ~ error, but there may be hints here ...

I started MySQL 5.7 with docker-compose and tried to connect Can I check it by connecting to mysql?

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
[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
Build a development environment for Docker + Rails6 + Postgresql
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