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