2017-03-31 48 views
0

不刪除數據,我有以下遷移:onDelete(「串聯」)上的數據透視表

public function up() 
    { 
     Schema::create('topics_to_subscriptions', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 
      $table->integer('topic_id')->unsigned(); 
      $table->integer('subscription_id')->unsigned(); 

      $table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade'); 
      $table->foreign('subscription_id')->references('id')->on('subscriptions')->onDelete('cascade'); 
     }); 
    } 

我undersatnding是使用onDelete('cascade')的時候,如果我刪除訂閱,則所有相關的TopicsToSubscriptions將被刪除。

當我運行App\Subscription::truncate();所有訂閱從subscriptions表正確刪除,但沒有數據從topics_to_subscriptions刪除。我究竟做錯了什麼?

回答

1

您不應該能夠截斷由外鍵引用的表。我懷疑你的外鍵從來沒有得到正確應用。

https://laravel.com/docs/5.4/migrations#foreign-key-constraints

public function up() 
{ 
    Schema::create('youtube_topics_to_subscriptions', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->integer('topic_id')->unsigned(); 
     $table->integer('youtube_subscription_id')->unsigned(); 

     $table->foreign('topic_id')->references('id')->on('youtube_topics')->onDelete('cascade'); 
     $table->foreign('youtube_subscription_id')->references('id')->on('youtube_subscriptions')->onDelete('cascade'); 
    }); 
}