Why is creating Foreign Key in Laravel 5.8 failing?

后端 未结 2 690
情话喂你
情话喂你 2021-01-17 17:55

The migration script below was running smoothly in an older version of Laravel but I added it to my fresh Laravel 5.8 and ran the script. I\'m getting Error: foreign k

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-17 18:32

    As we discussed in the comments above, a foreign key column must be the same data type as the primary key it references.

    You declared your user.id primary key as $table->bigIncrements('id') which becomes BIGINT UNSIGNED AUTO_INCREMENT in MySQL syntax.

    You must declare the foreign key as $table->unsignedBigInteger('user_id') which will become BIGINT UNSIGNED in MySQL, making it compatible with being a foreign key to the user.id column.

提交回复
热议问题