【rails】migrateできない。 ActiveRecord::NoDatabaseError: Unknown database【rake aborted!】

#マイグレートできない問題が発生
マイグレートしようとすると


```
bundle exec rake db:migrate
```

下記のようなエラーが出る。
**ActiveRecord::NoDatabaseError: Unknown database**ですか。
なんだかデータベースの何かに問題があるようです。
データベースを見てみましたが、作成はされているけど...まぁmigrate前だからテーブルはないよね。
と、いうようなよくみる光景でした。

```
bundle exec rake db:migrate

rake aborted!
ActiveRecord::NoDatabaseError: Unknown database 'exp-exam-pictweet_development'
/Users/okakaori/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/okakaori/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'

Caused by:
Mysql2::Error: Unknown database 'exp-exam-pictweet_development'
/Users/okakaori/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/okakaori/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
okakaorinoMacBook-Air:exp-exam-pictweet okakaori$ 
okakaorinoMacBook-Air:exp-exam-pictweet okakaori$ bundle exec rake db:seed

rake aborted!
ActiveRecord::NoDatabaseError: Unknown database 'exp-exam-pictweet_development'
/Users/okakaori/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/okakaori/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'

Caused by:
Mysql2::Error: Unknown database 'exp-exam-pictweet_development'
/Users/okakaori/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/okakaori/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
Tasks: TOP => db:abort_if_pending_migrations
(See full trace by running task with --trace)
okakaorinoMacBook-Air:exp-exam-pictweet okakaori$ 
okakaorinoMacBook-Air:exp-exam-pictweet okakaori$ rails s
=> Booting WEBrick
=> Rails 5.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
[2019-11-10 12:56:02] INFO  WEBrick 1.4.2
[2019-11-10 12:56:02] INFO  ruby 2.5.1 (2018-03-29) [x86_64-darwin18]
[2019-11-10 12:56:02] INFO  WEBrick::HTTPServer#start: pid=14694 port=3000
Started GET "/" for ::1 at 2019-11-10 12:56:13 +0900
  
ActiveRecord::NoDatabaseError (Unknown database 'exp-exam-pictweet_development'):
  
activerecord (5.2.1) lib/active_record/connection_adapters/mysql2_adapter.rb:26:in `rescue in mysql2_connection'
activerecord (5.2.1) lib/active_record/connection_adapters/mysql2_adapter.rb:12:in `mysql2_connection'
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:809:in `new_connection'
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `checkout_new_connection'
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:832:in `try_to_checkout_new_connection'
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:793:in `acquire_connection'
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:521:in `checkout'
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:380:in `connection'
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:1008:in `retrieve_connection'
activerecord (5.2.1) lib/active_record/connection_handling.rb:118:in `retrieve_connection'
activerecord (5.2.1) lib/active_record/connection_handling.rb:90:in `connection'
activerecord (5.2.1) lib/active_record/migration.rb:554:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.1) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.1) lib/rails/rack/logger.rb:26:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.7) lib/rack/method_override.rb:22:in `call'
rack (2.0.7) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.7) lib/rack/sendfile.rb:111:in `call'
railties (5.2.1) lib/rails/engine.rb:524:in `call'
rack (2.0.7) lib/rack/handler/webrick.rb:86:in `service'
/Users/okakaori/.rbenv/versions/2.5.1/lib/ruby/2.5.0/webrick/httpserver.rb:140:in `service'
/Users/okakaori/.rbenv/versions/2.5.1/lib/ruby/2.5.0/webrick/httpserver.rb:96:in `run'
/Users/okakaori/.rbenv/versions/2.5.1/lib/ruby/2.5.0/webrick/server.rb:307:in `block in start_thread'
::1 - - [10/Nov/2019:12:56:13 JST] "GET / HTTP/1.1" 500 88719
- -> /
::1 - - [10/Nov/2019:12:56:14 JST] "GET /favicon.ico HTTP/1.1" 200 0
http://localhost:3000/ -> /favicon.ico
^C[2019-11-10 13:01:28] INFO  going to shutdown ...
[2019-11-10 13:01:28] INFO  WEBrick::HTTPServer#start done.
Exiting
```

#解決方法
今回私が解決した方法は「database.yml」の修正でした。

``` database.yml

# ~~~~~省略~~~~~

development:
  <<: *default
  database: exp-exam-pictweet_development

# ~~~~~省略~~~~~

test:
  <<: *default
  database: exp-exam-pictweet_test

# ~~~~~省略~~~~~

production:
  <<: *default
  database: pictweet_hoge_production
  username: pictweet_hoge
  password: <%= ENV['PICTWEET_HOGE_DATABASE_PASSWORD'] %>

```

ymlは**「rake db:create」**をするときに、どういう感じのデータベースをmysqlに反映するか？
的なことが書いてあるっぽいですが、そこに間違いがあったようです。

「exp-exam-pictweet」というフォルダーで環境構築がしたいのですが、上記の記述に仲間はずれがいました。

``` database.yml
production:
  <<: *default
  database: pictweet_hoge_production
  username: pictweet_hoge
  password: <%= ENV['PICTWEET_HOGE_DATABASE_PASSWORD'] %>
```
上記の部分を他の項目に合わせて、下記のように修正しました。

``` database.yml
production:
  <<: *default
  database: exp-exam-pictweet_production
  username: exp-exam-pictweet
  password: <%= ENV['EXP_EXAM_PICTWEET_DATABASE_PASSWORD'] %>

```

##もう一度dbを作り直す

```
rake db:reset
```
**migrate、drop、createを一気に実行**できるコマンドです。めっちゃ便利。

これを行ってから再度**rake db:migrate**します。これで無事migrateできました。

#参考記事❤️
rake or rails コマンドでdbを扱うときのメモ
https://qiita.com/pugiemonn/items/e237b73025d8db8545bf