0
0

More than 3 years have passed since last update.

HerokuでWe're sorry, but something went wrong.とエラー出た場合の対処法

Last updated at Posted at 2021-08-06

 はじめに

Herokuに追加機能を実装した後、サイトを開くと下記のエラー表示が出たので
その時の対処法を記録します。

スクリーンショット 2021-08-06 18.11.01.png

これだけでは何のエラーが出ているのか分からないので、原因を探るためにエラーログを確認します。

エラーログの確認

まずはアプリケーションの内容を確認するために
ターミナルで以下のコマンド入力

$ heroku apps:info

=== sample-123456
Addons:         cleardb:ignite
Auto Cert Mgmt: false
Dynos:          web: 1
Git URL:        https://git.heroku.com/sample-123456.git
Owner:          sample@sample.com
Region:         us
Repo Size:      165 KB
Slug Size:      56 MB
Stack:          heroku-18
Web URL:        https://sample-123456.herokuapp.com/

sample-123456の部分がアプリケーション名です。
次に、下記のコマンドを入力してログを表示しましょう。
ターミナル

$ heroku logs --tail --app <<アプリケーション名>>

今回はアプリケーション名がsample-123456なので

$ heroku logs --tail --app sample-123456

とコマンドを入力します。
すると以下のようにログが表示されます。

2021-08-06T08:43:35.997532+00:00 app[web.1]: F, [2021-08-06T08:43:35.997422 #4] 
FATAL -- : [3b521c23-ff54-428d-963b-97b02366765e]
2021-08-06T08:43:35.997554+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e] ActionView::Template::Error (Mysql2::Error: Table 
'heroku_3c9be7b4803e9b6.likes' doesn't exist):
2021-08-06T08:43:35.997555+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e]     52:
2021-08-06T08:43:35.997555+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e]     53:               <%# いいね機能 %>
2021-08-06T08:43:35.997556+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e]     54:                 <div class="likes">
2021-08-06T08:43:35.997556+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e]     55:                   <% if current_user.liked_by? 
(problem.id) %>
2021-08-06T08:43:35.997557+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e]     56:                     <p><%= link_to 'いいねを外す', 
destroy_like_path(problem), method: :DELETE %><%= problem.likes.count %></p>
2021-08-06T08:43:35.997558+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e]     57:                   <% else %>
2021-08-06T08:43:35.997558+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e]     58:                     <p><%= link_to 'いいね', 
create_like_path(problem), method: :POST %><%= problem.likes.count %></p>
2021-08-06T08:43:35.997559+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e]
2021-08-06T08:43:35.997559+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e] app/models/user.rb:15:in `liked_by?'
2021-08-06T08:43:35.997559+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e] app/views/problems/index.html.erb:55
2021-08-06T08:43:35.997560+00:00 app[web.1]: [3b521c23-ff54-428d-963b- 
97b02366765e] app/views/problems/index.html.erb:45
2021-08-06T08:43:36.001198+00:00 heroku[router]: at=info method=GET path="/" 
host=bouldering-395.herokuapp.com request_id=3b521c23-ff54-428d-963b- 
97b02366765e fwd="180.147.68.165" dyno=web.1 connect=1ms service=710ms 
status=500 bytes=1827 protocol=https

ログの4行目を見るとActionView::Template::Error (Mysql2::Error: Table
'heroku_3c9be7b4803e9b6.likes' doesn't exist):
とあります。
要約すると「likesテーブルが存在していません」という意味です。

テーブルはマイグレーションファイルを実行することで作成されます。

ローカル環境では正常に動いていて、本番環境(Heroku)の方でみマイグレーションの実行ができていないという可能性が高そうです。

Herokuでマイグレーションファイルの実行をしましょう。
ターミナル

$ heroku run rails db:migrate

リロードすると正常に動きました。

まとめ

今回は追加でテーブルを作成したのにHeroku上でマイグレーションの実行をしていないためにエラーが出ていました。

エラーが出た場合
1. エラーログの出力
2. 最新のログ確認
3. ログを元に仮設検証していきましょう

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