0
0

More than 3 years have passed since last update.

$ bundle exec rake db:migrate RAILS_ENV=production時のエラーに関して

Posted at

はじめに

作成したアプリをAWSにデプロイしようと作業しており、「$ bundle exec rake db:migrate RAILS_ENV=productionコマンドを入力した際に発生したエラーを解決するまでの道のりです。
同じエラー内容が出ている方の一助になれれば幸いです。

エラー解決に向けて

unicornをインストールして、「$ bundle exec rake db:migrate RAILS_ENV=production」コマンドを入力すると、

ec2-user@ip-10-0-0-42 アプリ名]$ bundle exec rake db:migrate RAILS_ENV=production
rake aborted!
ActiveRecord::AdapterNotSpecified: 'production' database is not configured. Available: ["default", "development", "test"]

とエラー表示がされました。

どうも'production' databaseに問題がありそうです。そこでdatabase.ymlを確認してみます。

config/database.yml
〜省略〜
  production:
  <<: *default
  database: <%= Rails.application.credentials.db[:database] %>
  username: <%= Rails.application.credentials.db[:username] %>
  password: <%= Rails.application.credentials.db[:password] %>
  socket: <%= Rails.application.credentials.db[:socket] %>

production databaseあるじゃん!と思い、別の要因を探っていました。
しかし原因はymlファイルのインデントの付け方にありました。
正しくは以下のようになります。

config/database.yml
〜省略〜
  production:
   <<: *default
   database: <%= Rails.application.credentials.db[:database] %>
   username: <%= Rails.application.credentials.db[:username] %>
   password: <%= Rails.application.credentials.db[:password] %>
   socket: <%= Rails.application.credentials.db[:socket] %>          

ここでの注意点は、インデントはタブではなく、半角スペースを指定してあげてください。
もしここが上手くいっていないと、「Please note that YAML must be consistently indented using spaces. Tabs are not allowed. 」とエラーになってしまいます。

これで再度「bundle exec rake db:load_config RAILS_ENV=production」コマンドを入力したところ、またエラー表示が…。

== 20210320002528 CreateItems: migrating ======================================
-- create_table(:items)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Failed to open the referenced table 'categories': CREATE TABLE `items` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(255), `image` varchar(255), `category_id` bigint, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL,  INDEX `index_items_on_category_id`  (`category_id`), CONSTRAINT `fk_rails_89fb86dc8b`
FOREIGN KEY (`category_id`)
  REFERENCES `categories` (`id`)
)
/var/www/rails/men-skincare/db/migrate/20210320002528_create_items.rb:3:in `change'
/home/ec2-user/.rbenv/versions/2.5.3/bin/bundle:23:in `load'
/home/ec2-user/.rbenv/versions/2.5.3/bin/bundle:23:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Failed to open the referenced table 'categories': CREATE TABLE `items` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(255), `image` varchar(255), `category_id` bigint, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL,  INDEX `index_items_on_category_id`  (`category_id`), CONSTRAINT `fk_rails_89fb86dc8b`
FOREIGN KEY (`category_id`)
  REFERENCES `categories` (`id`)
)
/var/www/rails/men-skincare/db/migrate/20210320002528_create_items.rb:3:in `change'
/home/ec2-user/.rbenv/versions/2.5.3/bin/bundle:23:in `load'
/home/ec2-user/.rbenv/versions/2.5.3/bin/bundle:23:in `<main>'
Caused by:
Mysql2::Error: Failed to open the referenced table 'categories'
/var/www/rails/men-skincare/db/migrate/20210320002528_create_items.rb:3:in `change'
/home/ec2-user/.rbenv/versions/2.5.3/bin/bundle:23:in `load'
/home/ec2-user/.rbenv/versions/2.5.3/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

うーん、itemsテーブルの作成時にエラーが発生しているようです。
もっと詳しくエラー内容を確認してみると、itemsテーブルを作ろうとしている時に、categoriesテーブルの参照がないよと言われています。
itemsテーブルとcategoriesテーブルの作成順番がうまくいっていないようなので、一度categoryモデルごと削除、再度マイグレーションして、無事エラー解消することができました。

終わりに

ymlファイルのインデント規約に関しては知らなかったので良い勉強となりました。
もっとLinuxの知識をつける必要があると感じました。

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