前提・実現したいこと
railsアプリケーションに、モデルの変更があった為マイグレーションファイルを作成→カラムを追加→db:migrateをおこなった。
この時、
開発環境では問題なく動作ができた。しかし、本番環境のHEROKUではエラーが出てしまった。
状況は、以下です。
①Usersモデル
にpassword_confirmation
カラムを追加する必要が出る。
②ローカル環境で、マイグレートまで行う。
③ローカル環境で動作を確認済み
④変更をHEROKUへPush
⑤エラーが出た。
エラーの解決方法を知りたいです。
発生している問題・エラーメッセージ
エラー文
\HomeworkManager>heroku run rake db:migrate RAILS_ENV=production Running rake db:migrate RAILS_ENV=production on ⬢ homeworkmanagermkbkzm... up, run.4771 (Free) D, [2020-02-26T18:31:47.366063 #4] DEBUG -- : (0.8ms) SELECT pg_try_advisory_lock(2413219826671668840) D, [2020-02-26T18:31:47.380448 #4] DEBUG -- : (1.5ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC I, [2020-02-26T18:31:47.381686 #4] INFO -- : Migrating to AddPasswordConfirmationForUsers (20200226092416) D, [2020-02-26T18:31:47.384397 #4] DEBUG -- : (0.9ms) BEGIN == 20200226092416 AddPasswordConfirmationForUsers: migrating ================== -- add_column(:Users, :password_confirmation, :string) D, [2020-02-26T18:31:47.387105 #4] DEBUG -- : (2.1ms) ALTER TABLE "Users" ADD "password_confirmation" character varying D, [2020-02-26T18:31:47.388015 #4] DEBUG -- : (0.7ms) ROLLBACK D, [2020-02-26T18:31:47.389036 #4] DEBUG -- : (0.8ms) SELECT pg_advisory_unlock(2413219826671668840) rake aborted! StandardError: An error has occurred, this and all later migrations canceled: PG::UndefinedTable: ERROR: relation "Users" does not exist : ALTER TABLE "Users" ADD "password_confirmation" character varying /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `exec' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `block (2 levels) in execute' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.1/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:74:in `block in execute' --中略-- /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command' /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch' /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli.rb:27:in `dispatch' /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start' /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli.rb:18:in `start' /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/exe/bundle:30:in `block in <top (required)>' /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors' /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/exe/bundle:22:in `<top (required)>' /app/bin/bundle:3:in `load' /app/bin/bundle:3:in `<main>' Caused by: PG::UndefinedTable: ERROR: relation "Users" does not exist /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `exec' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `block (2 levels) in execute' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
rake db:migrate:status の結果
>heroku run rake db:migrate:status ' Running rake db:migrate:status on ⬢ homeworkmanagermkbkzm... up, run.6434 (Free) database: d7nhi2qqsle2k8 Status Migration ID Migration Name -------------------------------------------------- D, [2020-02-26T19:01:46.752714 #4] DEBUG -- : (3.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC up 20200224155147 Create hindexts up 20200224160038 Create users up 20200224160636 Create subject tables up 20200224160754 Create time tables down 20200226092416 Add password confirmation for users
該当のソースコード
UP状態の親のテーブル
ruby:
1# ファイル名:20200224160038_create_users.rb 2class CreateUsers < ActiveRecord::Migration[5.2] 3 def change 4 create_table :users do |t| 5 t.string "name" 6 t.string "address" 7 t.string "class_code" 8 t.integer "studentnumber" 9 t.string "school" 10 t.string "password" 11 t.timestamps 12 end 13 end 14end 15
down状態のマイグレートしたいファイル
ruby:
1# ファイル名:20200226092416_add_password_confirmation_for_users.rb 2class AddPasswordConfirmationForUsers < ActiveRecord::Migration[5.2] 3 def change 4 add_column :Users,:password_confirmation,:string 5 end 6end 7 8
試したこと
ネットで検索しましたが、参照元のテーブルがないのが問題というものがほとんどで、今回のケースに一致しないと考えています。
補足情報(FW/ツールのバージョンなど)
Rails 5.2.4.1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/27 02:09
2020/02/27 08:17