简体   繁体   English

可空列作为laravel中的外键

[英]Nullable column as foreign key in laravel

I have customers table in the database. 我在数据库中有customer表。 A customer is associated with an industry. 客户与行业相关联。 Because of the requirement, industry_id is null when customer is created. 由于要求,在创建客户时, industry_id为null。 later it will be updated and correct industry id is added. 稍后它将被更新并添加正确的行业ID。

Now, when I want to add this column as foreign key it shows following error. 现在,当我想将此列添加为外键时,它会显示以下错误。

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table customers add constraint customers_industry_id_foreign foreign key ( industry_id ) references industries SQLSTATE [HY000]:一般错误:1215无法添加外键约束(SQL:alter table customers add constraint customers_industry_id_foreign外键( industry_id )引用industries
( id )) id ))

I have following code in customers migration. 我在客户迁移中有以下代码。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCustomersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->bigIncrements('id')->comment="Customer Identifier";
            $table->bigInteger('customer_category_id')->unsigned()->comment="Customer Category Identifier";
            $table->bigInteger('industry_id')->unsigned()->nullable()->comment="Industry Identifier";
            $table->string('email')->unique()->comment="Customer Email";
            $table->timestamps();

            $table->foreign('industry_id')->references('id')->on('industries');
            $table->foreign('customer_category_id')->references('id')->on('customer_categories');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customers');
    }
}

Here is industries migration. 这是行业迁移。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateIndustriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('industries', function (Blueprint $table) {
            $table->bigIncrements('id')->comment="Industry Indetifier";
            $table->string('name')->comment="Industry Name";
            $table->boolean('status')->default(true)->comment="Active or Inactive";
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('industries');
    }
}

Is it impossible to achieve what I want ? 实现我想要的是不可能的? or it is just illogical ? 或者它只是不合逻辑? If I am able to add foreign key then I can take various advantages of using foreign key. 如果我能够添加外键,那么我可以利用外键的各种优点。

将行业表中的id(primary_key)更改为唯一键,然后尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM