Laravel migration can't create foreign key

人盡茶涼 提交于 2019-12-24 20:46:46

问题


I'm trying to create a foreign key, artisan does not show me any errors, just does not create my foreign key, it's probably my code:

1° table:

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

the foreign key table

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:


Change your code from

$table->integer('id_coop')->unsigned;
$table->foreign('id_coop')->references('id')->on('cooperados');

To:

$table->unsignedBigInteger('id_coop')->index();
$table->foreign('id_coop')->references('id')->on('cooperados')->onDelete('cascade');

Or you can also use below code

 $table->integer('id_coop')->unsigned()->index();
 $table->foreign('id_coop')->references('id')->on('cooperados')->onDelete('cascade');

Or also you can use below

$table->bigInteger('id_coop')->unsigned()->index();
$table->foreign('id_coop')->references('id')->on('cooperados')->onDelete('cascade');


来源:https://stackoverflow.com/questions/56395229/laravel-migration-cant-create-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!