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

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?