0
0

More than 1 year has passed since last update.

Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails

Posted at

Herokuへデプロイ中にタイトル通りのエラーが発生しました。

原因

おそらく、テーブルのカラムを変更したことです。ローカル環境では、rails db:rollbackでしっかりとテーブルをダウンさせた上で、テーブルの設計を変更していましたが、heroku上でrollbackすることまで考えが及ばなかったです。

解決

①手っ取り早く、Heroku上でテーブルをリセットします。

ターミナル
heroku run rails db:reset

注意点:リセットすると、登録されていたデータが全て消えます。

②以下のエラーが発生します

rails aborted!
ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
If you are sure you want to continue, run the same command with the environment variable:
DISABLE_DATABASE_ENVIRONMENT_CHECK=1

内容は「本当にDBをリセットしていいんですか?いいなら、”variable:”以降ののコマンドをさっきのコードに付け加えてください」というものです。

③再度、コードを実行

ターミナル
heroku run rails db:reset DISABLE_DATABASE_ENVIRONMENT_CHECK=1

以上でHeroku上でのDBリセット完了です。

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