【发布时间】:2021-01-09 11:58:45
【问题描述】:
我正在尝试迁移我的表,但是当我进行 laravel 迁移时,会显示一个错误。
SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:alter table subscribers 添加约束 subscribers_user_id_foreign 外键 (user_id) 引用 users (id) 在删除集 null )
这是我要迁移的表:
Schema::create('subscribers', function (Blueprint $table) {
$table->increments('id');
$table->string('account_no');
$table->enum('type',['individual','company'])->default('individual');
$table->string('title')->nullable();
$table->string('firstname');
$table->string('middlename');
$table->string('lastname');
$table->string('suffix')->nullable();
$table->enum('gender',['male','female']);
$table->date('birthday')->nullable();
$table->string('contact_no');
$table->string('email');
$table->tinyInteger('status')->default(1);
$table->integer('user_id')->unsigned();
$table->unsignedInteger('created_by');
$table->unsignedInteger('updated_by');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
$table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');
});
【问题讨论】:
-
你可以在这里找到答案stackoverflow.com/questions/22615926/…