Reputation: 10896
I'm building a ticketsystem with laravel. But when I remove a ticket the reactions don't go away?
This is my migration of the pivot table:
public function up()
{
Schema::create('reactions_ticket',function(Blueprint $table)
{
$table->integer('ticket_id')->unsigned();
$table->foreign('ticket_id')->references('id')->on('ticket')->onDelete('cascade');
$table->integer('reactions_id')->unsigned();
$table->foreign('reactions_id')->references('id')->on('reactions')->onDelete('cascade');
});
}
And this is my reaction table:
public function up()
{
Schema::create('reactions',function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->text('content');
$table->timestamps();
});
}
and my ticket table:
public function up()
{
Schema::create('ticket',function(Blueprint $table)
{
$table->increments('id');
$table->string('slug')->nullable();
$table->integer('ticketid')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('subject_id')->unsigned();
$table->foreign('subject_id')->references('id')->on('subject')->onDelete('cascade');
$table->integer('websites_id')->unsigned();
$table->foreign('websites_id')->references('id')->on('websites')->onDelete('cascade');
$table->integer('status_id')->unsigned();
$table->foreign('status_id')->references('id')->on('status')->onDelete('cascade');
$table->text('content');
$table->timestamps();
});
}
What am I doing wrong>?
Upvotes: 2
Views: 630
Reputation: 3679
You have a many to many join between reactions and tickets. Therefore a ticket can have many reactions and a reaction could have many tickets. If you delete a ticket I would not expect to have the reactions deleted because those reactions could be attached to other tickets that are not deleted.
What will be deleted are the entries in the pivot table for that ticket. If you wanted to clean up all reactions that no longer have a ticket attached, that would be a separate task, mysql won't cascade that for you.
Your table names are not standard, Btw. They should be tickets, reactions, and reaction_ticket for the pivot table.
Upvotes: 0
Reputation: 3421
I would always prefer to handle such actions by myself instead of let id be done by MySQL.
You could use laravels event-handler inside your Ticket
model.
protected static function boot() {
parent::boot();
static::deleting(function($ticket) {
// delete related stuff ;)
$reaction_ids = $ticket->reactions()->lists('id');
Reaction::whereIn($reaction_ids)->delete();
});
}
So you still have (if you want/need) the advantage of softdelete and more controll.
Upvotes: 4