Add "ON DELETE CASCADE" To Existing Column In Laravel


Answer :

Drop foreign key first. Thanks to Razor for this tip

$table->dropForeign('answers_user_id_foreign'); $table->foreign('user_id') ->references('id')->on('users') ->onDelete('cascade'); 

$table->foreign('user_id')       ->references('id')->on('users')       ->onDelete('cascade'); 

In my case, i'll need to put the col name in an array else that will be an error.

Schema::table('transactions', function (Blueprint $table) {     $table->dropForeign(['transactions_order_id_foreign']);     $table->foreign('order_id')         ->references('id')->on('orders')         ->onDelete('cascade')         ->change(); }); 

mysql 5.7 ver


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable