0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-09-01

クリニックとそこに勤務するスタッフのDB作成と関連付けを行う。

rails 5系
ruby 2.4.5
Docker
下記環境

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:

として、Activerecordのテーブル関連付けを行っております。

まずは、staffテーブルを

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

として作り、

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

外部キーとNN制約とUQ制約をつけました。

clinicテーブルとして

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

同様に、外部キーとNN制約とUQ制約をつけた。

最終的に、

docker-compose run web bundle exec rails db:migrate

マイグレーションをすると、

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$

となりった。
これはマイグレーションは成功しているのか?
rails db:migrateした際のエラー文 Index name ○○ on table テーブル名 already existsの解決法
この前に、Index〜〜のエラーがでたが、ここらへんにヒントがあるかもしれない、、、

docker-composeでMySQL5.7を起動して接続してみた
mysqlに接続すれば、確認できるのだろうか?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?