LoginSignup
1
0

More than 3 years have passed since last update.

Laravel 8.25.0でMigration時のSQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraintエラーを解決するまでの備忘録

Posted at

背景

とあるゲームのクランメンバー募集サイトを作っていて、Users、Threads(板 スレ)、Commentsの三つのテーブルで外部キー連携しようとした際にエラーが出た。
エラーの内容はこれ

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint 
(SQL: alter table `comments` add constraint `comments_user_id_foreign` 
foreign key (`user_id`) references `users` (`id`) on delete cascade on update cascade)

外部キーが追加できないよーとのことなので、とりあえずググってみた。

結論

こちらの記事にもあるように、連携したいテーブルのカラム同士でidの型が違っていたことが原因。
Laravelの機能で作ったUsersのidは最初からBigIncrementsなので、user_isはbigIntegerを指定してあげないとダメらしい

1
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
1
0