php - Laravel 迁移无法创建外键
问题描述
我正在尝试创建外键,工匠没有向我显示任何错误,只是没有创建我的外键,这可能是我的代码:
1°表:
Schema::create('cooperados', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('nameCoop', 255);
$table->integer('numCoop');
$table->string('cpfCoop', 11);
$table->date('dtCad');
$table->date('dtDem')->nullable();
$table->text('description')->nullable();
$table->decimal('subscritas', 6, 2);
$table->decimal('integralizadas', 6,2)->nullable();
$table->decimal('aintegralizar', 6,2)->nullable();
$table->enum('status', ['ativo','inativo'])->default('ativo');
});
外键表
public function up()
{
Schema::create('mov', function (Blueprint $table) {
$table->bigIncrements('idMov');
$table->timestamps();
$table->integer('id_coop')->unsigned;
$table->foreign('id_coop')->references('id')->on('cooperados');
$table->decimal('valor', 6, 2);
});
}
解决方案
更改您的代码
$table->integer('id_coop')->unsigned;
$table->foreign('id_coop')->references('id')->on('cooperados');
至:
$table->unsignedBigInteger('id_coop')->index();
$table->foreign('id_coop')->references('id')->on('cooperados')->onDelete('cascade');
或者你也可以使用下面的代码
$table->integer('id_coop')->unsigned()->index();
$table->foreign('id_coop')->references('id')->on('cooperados')->onDelete('cascade');
或者你也可以在下面使用
$table->bigInteger('id_coop')->unsigned()->index();
$table->foreign('id_coop')->references('id')->on('cooperados')->onDelete('cascade');
推荐阅读
- powershell - Powershell,循环通过 CSV 并使用 Switch
- google-cloud-platform - 无法从 VPC 外部访问内部负载均衡器
- javascript - 尝试使用数组和对象在 JavaScript 中制作字典
- python - PyPDF2 的替代品
- mongodb - 路口查询:谁在我的同一个团队?
- amazon-web-services - aws sqs lambda:消息没有重新出现
- java - void 方法上的 JAX-RS 拦截器映射异常
- javascript - 在 react native 中使用可重用组件后,数据不会使用 onDataChange 更改
- json - 所有数据都在括号中而不是括号中
- c# - C# - 如何显示完整的 Windows 10 内部版本号?