28
5

PG::UndefinedTable: ERRORの解消方法

Last updated at Posted at 2024-07-31

PG::UndefinedTable: ERROR

外部キーの追加を行った際にPG::UndefinedTable: ERRORが発生。

エラー内容

PG::UndefinedTable: ERROR:  relation "モデル名" does not exist

$ rails db:migrate:statusで現在のmigrationファイルを確認

Status   Migration ID    Migration Name
--------------------------------------------------
 down   2024xxxxxxxxx1  Create 子
 down   2024xxxxxxxxx2  Create 親

外部キーを設定している場合、親のモデルからmigrationする必要があるが、デフォルトでは古い順から実行されるためエラーになる。

解消方法

以下コマンドで参照元である親モデルを先に実行する。

$ rails db:migrate:up VERSION=2024xxxxxxxxx2

その後、通常通り$ rails db:migrateを行えばOK。

28
5
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
28
5