前提・実現したいこと
Docker-composeを用いてrails5 + MySQL の環境構築を行い、webアプリを作ろうとしています。
下記手順に沿って環境構築を行い、最後の手順であるサーバの起動まで確認できました。
https://qiita.com/azul915/items/5b7063cbc80192343fc0
実現したいこととしては、
テーブル作成をし、ActiveRecordを用いてCRUD操作等を行いたいです。
が、
現在以下のエラーメッセージがでてしまい、migrateができずテーブルが作成できていません。
初めて自分で環境構築おこなってwebアプリ作成に挑戦しているため、
質問に不十分な点あったら申し訳ありません。
気になる点ございましたら、お気軽にご指摘よろしくおねがいします。
( また、現在DB作成できているのか、接続できているのか、等
どういう状態になっているのかが恥ずかしながらわからないので、
「試したこと」をご覧いただいてそこも解説いただけると非常に嬉しいです。。。。)
発生している問題・エラーメッセージ
rails g modelコマンドを実行し、migrationファイルが作成された状態で、
rails db:migrate または rake db:migrate を行うと以下のメッセージがでます。
rails aborted! ActiveRecord::StatementInvalid: Mysql2::Error: No database selected: CREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL PRIMARY KEY) /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:187:in `block (2 levels) in execute' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:186:in `block in execute' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_adapter.rb:581:in `block (2 levels) in log' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_adapter.rb:580:in `block in log' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/notifications/instrumenter.rb:23:in `instrument' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_adapter.rb:571:in `log' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:185:in `execute' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/mysql/database_statements.rb:28:in `execute' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:311:in `create_table' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/schema_migration.rb:29:in `create_table' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:1187:in `initialize' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:1036:in `new' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:1036:in `up' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:1011:in `migrate' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/tasks/database_tasks.rb:172:in `migrate' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <top (required)>' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/commands.rb:18:in `<top (required)>' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `block in require' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:257:in `load_dependency' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `require' bin/rails:4:in `<main>' Caused by: Mysql2::Error: No database selected /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:187:in `block (2 levels) in execute' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:186:in `block in execute' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_adapter.rb:581:in `block (2 levels) in log' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_adapter.rb:580:in `block in log' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/notifications/instrumenter.rb:23:in `instrument' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_adapter.rb:571:in `log' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:185:in `execute' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/mysql/database_statements.rb:28:in `execute' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:311:in `create_table' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/schema_migration.rb:29:in `create_table' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:1187:in `initialize' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:1036:in `new' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:1036:in `up' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:1011:in `migrate' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/tasks/database_tasks.rb:172:in `migrate' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <top (required)>' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/commands.rb:18:in `<top (required)>' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `block in require' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:257:in `load_dependency' /usr/local/bundle/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)
試したこと
・ActiveRecord::Base.connection_config コマンド実行
→DBの接続情報が取得できることを確認
・テーブル一覧確認
→下記の通り返却。(取得できているのでしょうか…?)
irb(main):002:0> ActiveRecord::Base.connection.tables (1.7ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
・マイグレーションの実行状況確認
→ 下記エラー
root@c56b8b3b0361:/app_name# rails db:migrate:status Schema migrations table does not exist yet.
・DBの中身確認するためにbundle exec rails db または rails dbconsole実行
→ 下記エラー
root@c56b8b3b0361:/app_name# rails dbconsole Traceback (most recent call last): 19: from bin/rails:4:in `<main>' ︙ 1: from /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/commands/dbconsole/dbconsole_command.rb:137:in `find_cmd_and_exec' /usr/local/bundle/gems/railties-5.2.4.2/lib/rails/commands/dbconsole/dbconsole_command.rb:137:in `exec': no implicit conversion of nil into String (TypeError)
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。