LoginSignup
3
1

More than 3 years have passed since last update.

HerokuのDB更新時のActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column ... does not existの対処方

Posted at

PG::UndefinedColumn: ERROR

ローカル環境に置けるrails db:migrateはエラーが起きなかったが、
本番環境(Heroku)に heroku run rails db:migrateをすると出るエラー

今回起きた要因

これが発生した要因として
今までは普通にできたが、不要なカラができてしまったので削除する。
そしてHerokuも同様に反映させようとしたが今回のエラーが発生。
ゆえに今回はカラム削除のDBファイルがからんでいると推測。

エラー内容 &対処法

PG::UndefinedColumn: ERROR: column "string" for relation "users" does not exist

とでていた。
そこでusersテーブルの削除したファイルを見てみると

..._remove_delete_to_users
 class RemoveDelete2ToUsers < ActiveRecord::Migration[5.1] 
   def change 
     remove_column :users, :job, :string 
     remove_column :users, :string, :string 
     remove_column :users, :basic_overtime_pay, :string 
     remove_column :users, :integer, :string 
     remove_column :users, :uid, :string 
     remove_column :users, :string, :string 
     remove_column :users, :image, :string 
     remove_column :users, :string, :string 
     remove_column :users, :encrypted_password, :string 
     remove_column :users, :string, :string 
     remove_column :users, :reset_password_token, :string 
     remove_column :users, :string, :string 
     remove_column :users, :note, :string 
     remove_column :users, :string, :string 
   end 
 end 

このようにカラムの型がたくさんある状態になっている。
そこでここを整理整頓してみる。

..._remove_delete_to_users
 class RemoveDelete2ToUsers < ActiveRecord::Migration[5.1] 
   def change 
     remove_column :users, :job, :string 
     remove_column :users, :basic_overtime_pay, :integer 
     remove_column :users, :uid, :string 
     remove_column :users, :image, :string
     remove_column :users, :encrypted_password, :string 
     remove_column :users, :reset_password_token, :string
     remove_column :users, :note, :string 
   end 
 end 

ここで改めて heroku run rails db:migrateをすると
通った!!!
ロールバックするさいもカラムと型が合わずにうまくいかない!
みたいなことがあるみたいなのです。
今回のように同じエラーがでてるかたは参考にしてみていただけると幸いです。

3
1
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
3
1