マイグレーションファイルに必要な記述をした後にrails db:migrateをすると以下のようなエラー文が出て困っています。
== 20201204124542 CreateComments: migrating =================================== -- create_table(:comments) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'protospace32566_development.user_ids' doesn't exist /Users/lein/projects/protospace32566/db/migrate/20201204124542_create_comments.rb:3:in `change' /Users/lein/projects/protospace32566/bin/rails:9:in `<top (required)>' /Users/lein/projects/protospace32566/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Table 'protospace32566_development.user_ids' doesn't exist /Users/lein/projects/protospace32566/db/migrate/20201204124542_create_comments.rb:3:in `change' /Users/lein/projects/protospace32566/bin/rails:9:in `<top (required)>' /Users/lein/projects/protospace32566/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Mysql2::Error: Table 'protospace32566_development.user_ids' doesn't exist /Users/lein/projects/protospace32566/db/migrate/20201204124542_create_comments.rb:3:in `change' /Users/lein/projects/protospace32566/bin/rails:9:in `<top (required)>' /Users/lein/projects/protospace32566/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Mysql2::Error: Cannot add foreign key constraint /Users/lein/projects/protospace32566/db/migrate/20201204124542_create_comments.rb:3:in `change' /Users/lein/projects/protospace32566/bin/rails:9:in `<top (required)>' /Users/lein/projects/protospace32566/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)
マイグレーションファイルの記述です
class CreateComments < ActiveRecord::Migration[6.0] def change create_table :comments do |t| t.text :text, null: false t.references :user_id, null: false, foreign_key: true t.references :prototype_id, null: false, foreign_key: true t.timestamps end end end
mysql2のエラーについても調べてみましたが解決せず‥
DB削除して一からやり直してもこのエラーが出ます。
どなたかご教授いただけると大変助かります。
ruby:2.6.5
Rails 6.0.3.4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/04 14:08