10
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[PG::UndefinedTable: ERROR: relation "XXXXXX" does not exist] への対応

Last updated at Posted at 2019-04-05

問題

heroku run rails db:migrateを行うと、こんなエラーがでた

PG::UndefinedTable: ERROR:  relation "param_types" does not exist
: CREATE TABLE "param_values" ("id" bigserial primary key, "name" character varying, "presentation" character varying, "param_type_id" bigint, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_e74ade1df5"
FOREIGN KEY ("param_type_id")
  REFERENCES "param_types" ("id")
)

ParamValueというテーブルを作ろうとしたら、
その中でParamTypeというテーブルのid(=param_types.id)が参照されている。

しかしParamTypeというテーブルは存在しない(=UndefinedTable)ので、
ParamValueより先に作ってください、ということ。

ローカルでmigrationできても、どうやらherokuでは
migration fileは日付順にmigrateされるらしい。

対処

ParamValueよりも先にParamTypeが作られるよう、
手動でmigration fileの日付を変更し
ローカルで正常にmigrateされるのを確認したあと、
herokuにpushした。

手順

  1. rails db:drop

    →この時点でmigration fileの日付を変更してよい
  2. rails db:create
  3. rails db:migrate
  4. 問題なければherokuにpush
  5. heroku pg:reset DATABASE
  6. heroku run rails db:migrate

おわり
10
7
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
10
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?