问题
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