1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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)

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

結論

[こちら](https://qiita.com/kitakrazy/items/6a3e1936da727dbd92b3)の記事にもあるように、連携したいテーブルのカラム同士で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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?