Firstly, my English very bad. Sorry about that.
I have a foreign key in cards
table like:
$table->foreignId('wallet_id')->constrained('wallets', 'id')->onDelete('cascade');
Since some reasons I need change cascade
to set null
on that column
I have try (in a new migration):
$table->dropForeign('cards_wallet_id_foreign');
$table->foreignId('wallet_id')
->nullable()
->onDelete('set null')
->change();
that run okey but when delete it not set null :((
How I can solve that. Thank you!!
CodePudding user response:
You can not just modify the existing migration, You need to make another one
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeSomeTableColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('the_table_you_are_changing', function (Blueprint $table) {
$table
->foreignId('wallet_id')
->constrained('wallets', 'id')
->onDelete('set null')
->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
CodePudding user response:
You must change your table migration schema in a new migration, and make sure you set the foreign key field as nullable:
$table->integer('wallet_id')->unsigned()->nullable();
and then use set null
like this:
$table->...->onDelete('set null');