Laravel迁移无法创建外键

时间:2019-05-31 12:34:07

标签: php mysql sql laravel artisan

我正在尝试创建外键,工匠不会向我显示任何错误,只是未创建我的外键,这可能是我的代码:

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);
        });
    }

1 个答案:

答案 0 :(得分:1)

更改您的代码

$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');