laravel migration best way to add foreign key

后端 未结 10 1095
梦毁少年i
梦毁少年i 2020-12-09 09:19

Simple question: I\'m new to Laravel. I have this migration file:

Schema::create(\'lists\', function(Blueprint $table) {
    $table->increments(\'id\'); 
         


        
10条回答
  •  情深已故
    2020-12-09 09:55

    If you want to add onDelete('cascade') on the existing foreign key, just drop the indexes and create them again:

    public function up()
    {
        Schema::table('lists', function($table)
        {
            $table->dropForeign('lists_user_id_foreign');
    
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }
    
    public function down()
    {
        Schema::table('lists', function($table)
        {
            $table->dropForeign('lists_user_id_foreign');
    
            $table->foreign('user_id')->references('id')->on('users');
        });
    }
    

提交回复
热议问题