Ruby on Rails でproblemsというテーブルからplaceというカラムを削除しようとし、マイグレーションファイルを実行したところ、以下のエラーが発生してしまいました。
発生している問題・エラーメッセージ
C:\Users\XXXX>rails db:migrate == 20180819025741 RemovePlaceToProblem: migrating ============================= -- remove_column(:problems, :place, :string) rails aborted! StandardError: An error has occurred, this and all later migrations canceled: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "problems" C:/Users/XXXX/db/migrate/20180819025741_remove_place_to_problem.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Caused by: ActiveRecord::InvalidForeignKey: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "problems" C:/Users/XXXX/db/migrate/20180819025741_remove_place_to_problem.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Caused by: SQLite3::ConstraintException: FOREIGN KEY constraint failed C:/Users/XXXX/db/migrate/20180819025741_remove_place_to_problem.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)
該当のソースコード
(↓) db\migrate\20180819025741_remove_place_to_problem.rb
class RemovePlaceToProblem < ActiveRecord::Migration[5.1] def change remove_column :problems, :place, :string end end
(↓)db\schema.rb
create_table "problems", force: :cascade do |t| t.string "name" t.string "grade" t.string "place" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "posts", force: :cascade do |t| t.text "URL" t.text "comment" t.integer "problem_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "approved" t.index ["problem_id"], name: "index_posts_on_problem_id" end
(↓)app\models\problem.rb
class Problem < ApplicationRecord has_many :posts, dependent: :destroy validates :name, presence: true, uniqueness: true validates :grade, presence: true end
(↓)app\models\post.rb
class Post < ApplicationRecord belongs_to :problem, dependent :destroy validates :URL, presence: true, uniqueness: true validates :comment, length: { maximum: 500 } end
エラーメッセージを読むと、外部キー制約に関連してエラーが発生しているようだということは分かったのですが、どのようにすればエラーを起こさず削除できるのかが分かりません。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/09/02 04:40