Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lilsOfi's avatar

Cascading delete not working

So, i have 3 tables MacroAreas, Specific_areas and Services. They're related to each other by one to many relationships. So MacroAreas has many specific areas and so on. I'll copy the migrations here. What I need is that when I delete a "parent" like macroArea it also deletes the related records on specifc area. If I got it right is how the cascading delete should work, no? So i cannot understand what's wrong here...

Migrations

  Schema::create('macro_areas', function (Blueprint $table) {
            $table->id();
            $table->foreignId('regione_id')->constrained()->onDelete('cascade');
            $table->string('nome');
            $table->timestamps();
            $table->index('nome');
        });
  Schema::create('specific_areas', function (Blueprint $table) {
            $table->id();
            $table->foreignId('macroArea_id')->constrained()->onDelete('cascade');
            $table->string('nome');
            $table->timestamps();
            $table->index('nome');
        });

 Schema::create('services', function (Blueprint $table) {
            $table->id();
            $table->foreignId('specificArea_id')->constrained()->onDelete('cascade');
            $table->string('denominazione')->default('');
            $table->string('indirizzo')->nullable();
            $table->string('comune')->default('');
            $table->integer('cap')->default(0);
            $table->string('email')->nullable();
            $table->string('pec')->nullable();
            $table->string('telefono')->nullable();
            $table->string('fax')->nullable();
            $table->longText('descrizione')->nullable();
            $table->string('sito')->nullable();
            $table->timestamps();
            $table->index('denominazione');
            $table->index('comune');
        });

I'll paste the models as well for clarity macroArea


    public function specific_areas()
    {
        return $this->hasMany(SpecificArea::class, 'macroArea_id');
    }

specific

  public function macroArea()
    {
        return $this->belongsTo(MacroArea::class, 'macroArea_id');
    }

    public function services()
    {
        return $this->hasmany(Service::class, 'specificArea_id');
    }

services

   public function specificArea()
    {
        return $this->belongsTo(SpecificArea::class, 'specificArea_id');
    }
0 likes
8 replies
tykus's avatar
tykus
Best Answer
Level 104

You can be explicit with the table name in the constrained function(s) when your foreign keys do not follow Laravel's naming conventions:

Schema::create('specific_areas', function (Blueprint $table) {
    //...
    $table->foreignId('macroArea_id')->constrained('macro_areas')->onDelete('cascade');
Schema::create('services', function (Blueprint $table) {
    //...
    $table->foreignId('specificArea_id')->constrained('specific_areas')->onDelete('cascade');
1 like
tykus's avatar

What database are you using; does it support foreign key constraints and cascading deletes (SQLite does not by default)

lilsOfi's avatar

@tykus oh, I use MySql with innoDb engine which should support cascading deletes

tykus's avatar

@lilsOfi you did re-run the migrations after explicitly referencing the table name(s), right?

Please or to participate in this conversation.