执行迁移时创建外键失败
创建迁移时框架会提示无法创建外键约束。
根据查找的资料,添加外键约束需要主键与外键的字段类型一致,这里 product_id
字段的 unsignedBigInt
应该需要修改为 unsignedInteger
才可以吧?我测试的结果也是修改为 unsignedInteger
可以执行成功。
Schema::create('seckill_products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id'); // 这里和 products 表的字段类型不一致
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->dateTime('start_at');
$table->dateTime('end_at');
});
推荐文章: