###現象
オリジナルアプリ製作中に起こった現象です。
部分テンプレートのヘッダーにログインボタンや新規登録ボタンがあるのですが、急に新規登録ボタンのnew_user_registration_pathのルーティングがなくなっていました。
なので調べたところあまり同じ現象になったことがある人は少なく参考にする記事がないので、もう一度deviseを入れ直したりしたら治るのではと思いrails g devise user などを行っていました。
それでも治らず、、、
そこで、一度テーブルを消してから(マイグレーションファイルがあるためやり直せると思った)もう一度rails db:migrateをすればやり直せるのではと思ったので全テーブルを削除して再度rails db:migrateをおこないました。
そうしたら途中までしかテーブルが作られずターミナルでのエラーとgoogleのタブでのエラーが出てきました。
###エラー文
ターミナル
== 20201208032056 CreateItems: migrating ====================================== -- create_table(:items) rails aborted! StandardError: An error has occurred, all later migrations canceled: Column `user_id` on table `items` does not match column `id` on `users`, which has type `bigint(20)`. To resolve this issue, change the type of the `user_id` column on `items` to be :bigint. (For example `t.bigint :user_id`). Original message: Mysql2::Error: Cannot add foreign key constraint /Users/naganokouki/projects/Old_fashion/db/migrate/20201208032056_create_items.rb:3:in `change' /Users/naganokouki/projects/Old_fashion/bin/rails:9:in `<top (required)>' /Users/naganokouki/projects/Old_fashion/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ActiveRecord::MismatchedForeignKey: Column `user_id` on table `items` does not match column `id` on `users`, which has type `bigint(20)`. To resolve this issue, change the type of the `user_id` column on `items` to be :bigint. (For example `t.bigint :user_id`). Original message: Mysql2::Error: Cannot add foreign key constraint /Users/naganokouki/projects/Old_fashion/db/migrate/20201208032056_create_items.rb:3:in `change' /Users/naganokouki/projects/Old_fashion/bin/rails:9:in `<top (required)>' /Users/naganokouki/projects/Old_fashion/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/naganokouki/projects/Old_fashion/db/migrate/20201208032056_create_items.rb:3:in `change' /Users/naganokouki/projects/Old_fashion/bin/rails:9:in `<top (required)>' /Users/naganokouki/projects/Old_fashion/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)
googleタブのエラー
ActiveRecord::PendingMigrationError Migrations are pending. To resolve this issue, run: rails db:migrate RAILS_ENV=development
###行ったこと
ターミナルのエラーにColumn user_id
on table items
does not match column id
on users
, which has type bigint(20)
. To resolve this issue, change the type of the user_id
column on items
to be :bigint. (For example t.bigint :user_id
).というのがあるがこの場合reference型で現象が起こるまでは表示できていたので問題なさそうなので特に何もしていません。
googleタブのエラーの方はエラー画面にある”Run pending migrations”のグレーのボタンを押す
エラー文の指示通りに”rails db:migrate RAILS_ENV=development”を実行とあったので押してみたが、
An error has occurred, all later migrations canceled: Column user_id
on table items
does not match column id
on users
, which has type bigint(20)
. To resolve this issue, change the type of the user_id
column on items
to be :bigint. (For example t.bigint :user_id
). Original message: Mysql2::Error: Cannot add foreign key constraint
とターミナルのエラーと一緒。マイグレーションファイルの読み込み順がおかしいのではと思いusersテーブルとitemsテーブルの読み込みを日付を変えてもう一度読み込ませたが同じエラーが出ます。
エラーの解決策を教えていただきたいです。
長くてわかりにくいとおもいますが、ご協力お願いします。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/24 00:28