【发布时间】:2020-06-23 16:40:25
【问题描述】:
我似乎无法弄清楚为什么我的相同类型的 PK 和 FK 没有在 Laravel 中链接。 两个表都是 InnoDB。 第二个屏幕截图中的外键显示,即使 PK 表中不存在,我也可以输入任何整数。请帮忙。我的 FK 创建迁移代码如下:
证明 PK 和 FK 类型相同(左侧为会议桌;右侧为参与者桌:
完整性约束可以被打破的证明(我可以输入 9999999 等。我应该只能输入 1、2 或 3 作为 FK ID):
迁移文件:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateParticipantsTable extends Migration
{
public function up()
{
Schema::create('participants', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('meeting_id')->nullable();
$table->string('attendance');
$table->string('invited');
$table->foreign('meeting_id')
->references('id')
->on('meetings')
->onDelete('cascade');
$table->timestamps();
// $table->engine = "InnoDB";
});
}
public function down()
{
Schema::dropIfExists('participants');
}
}
【问题讨论】:
标签: mysql laravel-7 referential-integrity