mysql - Laravel 6 on Migration: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
问题描述
我试图在我的桌子上进行迁移,但是当我进行 laravel 迁移时,出现了一个错误。
SQLSTATE [HY000]:一般错误:1215 无法添加外键约束(SQL:alter table subscribers
add constraint subscribers_user_id_foreign
foreign key ( user_id
) references users
( id
) on delete set 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');
});
解决方案
现在试试
改变这个
$table->unsignedBigInteger('user_id');
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->unsignedBigInteger('user_id');
$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');
});
推荐阅读
- gcc - Autofdo 是否支持 -fprofile-reorder-functions?
- open-liberty - 到期时自动刷新 OpenId 访问令牌
- python - 如何在模型保存方法中获取多对多字段数据
- apache-spark - 使用 spark 编写 parquet 文件时设置数据类型
- python - 差值最接近特定数字的值
- mongodb - 在 mongoDB 连接字符串中指定多个服务器会阻止其连接,但仅指定主服务器有效
- c++ - 如何使用模板类作为同一模板类的类型参数
- python - 在多个列上应用 Pandas DataFram 中的条件
- internationalization - NextJS v10 + i18n - 用不同语言重写文件夹名称
- java - 在一个类中选择的事件侦听器实体,我想将它转移到另一个类