5
4

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.

Laravel8.xで外部制約キーありでNULL許可にする方法

Last updated at Posted at 2021-03-27

ハマったのでメモ。

だめな例

$table->foreignId('user_id')->constrained()->nullable();

OKな例

$table->foreignId('user_id')->nullable()->constrained();

constrainedの前にnullableを入れないとだめでした。ものすごくハマりました。

最初下記を参考にしましたが、8.xではだめだったので、どこかのタイミングで仕様が変わったんでしょうかね。

PHP - laravel nullを許容する外部キーを作成できるのか?|teratail
https://teratail.com/questions/298892

参考

php - Laravel Database Schema, Nullable Foreign - Stack Overflow
https://stackoverflow.com/questions/37735055/laravel-database-schema-nullable-foreign

PHP - laravel nullを許容する外部キーを作成できるのか?|teratail
https://teratail.com/questions/298892

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?