LoginSignup
0
0

【heroku/Rails】ActionView::Template::Error (PG::UndefinedTable: ERROR: relationに対処する

Posted at

heroku へbranchをpushした後、heroku openで表示されたもの。

このActionView::Template::Error (PG::UndefinedTable: ERROR: relationに対処します。

環境

Docker version 24.0.5, build ced0996
Rails 7.0.7.2
heroku/8.6.0 darwin-x64 node-v16.19.0

スクリーンショット 2023-10-20 0.50.01.png

結論:herokuでマイグレーションを行っていなかった

結論から先に述べると

rails db:migrate

マイグレーションをローカルでしか行っていなかったのが原因でした。

ローカルの環境から確認していきます。

まずエラー画面に何が表示されているかを確認すると以下の記述がありました。

PG::UndefinedTable: ERROR:  relation "items" does not exist
LINE 9:  WHERE a.attrelid = '"items"'::regclass

今回作成しているアプリではItemsテーブルを使用しています。

ローカルではマイグレーションを行なっているためitemsテーブルが存在していますが、herokuではマイグレーションを行なっていないため、

「itemsテーブルがないよ!」

と怒られている状態です。

マイグレーションの履歴を確認する

rails db:migirate:statusでマイグレーションの履歴を確認

ローカルでは

docker compose exec web rails db:migrate:status

database: myapp_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20230206140511  Create tasks
   up     20230907135931  Create active storage tablesactive storage
   up     20231001162251  Create items

マイグレーションが実行されているためupになっていますね。

なのでローカルでは各種テーブルが作成されているということ。

herokuでは

heroku run rails db:migrate:status
Running rails db:migrate:status on ⬢ app_name.... up, run.6261 (Basic)

database: ************

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20230206140511  Create tasks
  down    20230907135931  Create active storage tablesactive storage
  down    20231001162251  Create items

herokuの方を確認するとdownになっているものが2つあります。

ActiveStorageのGemを追加したときに作成されたマイグレーションファイルと、

itemsテーブルを作成するためのマイグレーションファイルはdownで実行されていません。

heroku run rails db:migrateを行う

マイグレーションを完了させるために上記コマンドを実行。

heroku run rails db:migrate
Running rails db:migrate on ⬢ app_name... up, run.6170 (Basic)
I, [2023-10-19T16:50:19.227626 #2]  INFO -- : Migrating to CreateActiveStorageTables (20230907135931)
== 20230907135931 CreateActiveStorageTables: migrating ========================
-- create_table(:active_storage_blobs, {:id=>:primary_key})
   -> 0.0315s
-- create_table(:active_storage_attachments, {:id=>:primary_key})
   -> 0.0128s
-- create_table(:active_storage_variant_records, {:id=>:primary_key})
   -> 0.0101s
== 20230907135931 CreateActiveStorageTables: migrated (0.0547s) ===============

I, [2023-10-19T16:50:19.295056 #2]  INFO -- : Migrating to CreateItems (20231001162251)
== 20231001162251 CreateItems: migrating ======================================
-- create_table(:items)
   -> 0.0054s
== 20231001162251 CreateItems: migrated (0.0055s) =============================

マイグレーションが無事完了したみたいです。

試しにheroku run bashでherokuコンテナへ入ってテーブルが作成されているかを確認します。

heroku run bashでコンテナへ入る

heroku run bashコマンドでherokuコンテナ内でlinuxコマンドが使えます。

rails cを起動してテーブルができているかどうかを確認。

heroku run bash
Running bash on ⬢ ancient-journey-08420... up, run.1689 (Basic)
~ $ rails c
Loading production environment (Rails 7.0.7.2)
irb(main):001:0> ActiveRecord::Base.connection.tables
=>
["active_storage_variant_records",
 "items",
 "active_storage_blobs",
 "active_storage_attachments",
 "ar_internal_metadata",
 "schema_migrations",
 "tasks"]
irb(main):003:0>

ちゃんとitemsテーブルが作成されています。

ただ初期データは入っていないのでseedファイルに記述している内容をdbに反映させます。

#bashでコンテナへ入っていないならターミナル上で以下のコマンド
heroku run rails db:seed

#コンテナ内部にいるなら以下のコマンド
rails db:seed

seedファイルに記述していた内容が無事反映されました。

念のため、データの確認を行います。

heroku run bash

rails c

Item.all
=>
[#<Item:0x00007f92ff1ac900
  id: 1,
  name: "テスト商品1",
  price: 1250,
  description: "これはテスト商品1についての説明です",
  item_image: nil,
  created_at: Thu, 19 Oct 2023 16:57:42.365419000 UTC +00:00,
  updated_at: Thu, 19 Oct 2023 16:57:42.428715000 UTC +00:00>,
 #<Item:0x00007f92ff1c6878
  id: 2,
  name: "テスト商品2",
  price: 1000,
  description: "これはテスト商品2についての説明です",
  item_image: nil,
  created_at: Thu, 19 Oct 2023 16:57:43.308090000 UTC +00:00,
  updated_at: Thu, 19 Oct 2023 16:57:43.319211000 UTC +00:00>,
 #<Item:0x00007f92ff1c67d8
  id: 3,
  name: "テスト商品3",
  price: 2000,
  description: "これはテスト商品3についての説明です",
  item_image: nil,
  created_at: Thu, 19 Oct 2023 16:57:43.655424000 UTC +00:00,
  updated_at: Thu, 19 Oct 2023 16:57:43.665658000 UTC +00:00>,
 #<Item:0x00007f92ff1c6738
  id: 4,
  name: "テスト商品4",
  price: 1300,
  description: "これはテスト商品4についての説明です",
  item_image: nil,
  created_at: Thu, 19 Oct 2023 16:57:44.163114000 UTC +00:00,
  updated_at: Thu, 19 Oct 2023 16:57:44.173476000 UTC +00:00>,
 #<Item:0x00007f92ff1c6698
  id: 5,
  name: "テスト商品5",
  price: 2100,
  description: "これはテスト商品5についての説明です",
  item_image: nil,
  created_at: Thu, 19 Oct 2023 16:57:44.532037000 UTC +00:00,
  updated_at: Thu, 19 Oct 2023 16:57:44.542294000 UTC +00:00>,
 #<Item:0x00007f92ff1c65f8
  id: 6,
  name: "テスト商品6",
  price: 800,
  description: "これはテスト商品6についての説明です",
  item_image: nil,
  created_at: Thu, 19 Oct 2023 16:57:44.879061000 UTC +00:00,
  updated_at: Thu, 19 Oct 2023 16:57:44.888875000 UTC +00:00>,
 #<Item:0x00007f92ff1c6558
  id: 7,
  name: "テスト商品7",
  price: 5000,
  description: "これはテスト商品7についての説明です",
  item_image: nil,
  created_at: Thu, 19 Oct 2023 16:57:45.226779000 UTC +00:00,
  updated_at: Thu, 19 Oct 2023 16:57:45.236941000 UTC +00:00>,
 #<Item:0x00007f92ff1c64b8
  id: 8,
  name: "テスト商品8",
  price: 650,
  description: "これはテスト商品8についての説明です",
  item_image: nil,
  created_at: Thu, 19 Oct 2023 16:57:45.632928000 UTC +00:00,
  updated_at: Thu, 19 Oct 2023 16:57:45.642601000 UTC +00:00>]

初期データが反映できたみたいなので

heroku openで確認。

無事表示された( ´ ▽ ` )

スクリーンショット 2023-10-20 2.03.34.png

まとめ

  • herokuへのデプロイ不慣れが招いた問題
  • localで反映させたマイグレーションがあるなら、heroku側でも同じようにマイグレーションが必要だということ
  • 細かく切り分けて原因を把握する必要がある
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