Skip to content

onlyTrashed()->forceDelete() seems to lose the SingleTableInheritance scope, deletes way more than intended #73

@jwadhams

Description

@jwadhams

I'm planning on working this until I have a proposed patch, but wanted to give a heads up and see if you had any advice.

Here's my reproduction case in my own codebase: (TP_TradeEstimate and Hours are both subclasses of the same thing)

    public function testOnlyTrashedShouldntBreakSingleTableInheritance(): void
    {
        $liveTP = TP_TradeEstimate::factory()->create();
        $deletedTP = TP_TradeEstimate::factory()->create();
        $deletedTP->delete();
        $liveOther = Hours::factory()->create();
        $deletedOther = Hours::factory()->create();
        $deletedOther->delete();

        self::assertSame(1, TP_TradeEstimate::onlyTrashed()->count());
        $affectedRows = TP_TradeEstimate::onlyTrashed()->forceDelete();
        self::assertSame(1, $affectedRows, 'Affected rows should match counted rows');

        self::assertModelExists($liveTP);
        self::assertFalse(
            DB::table('account_integrations')
                ->where('id', $deletedTP->id)
                ->exists(),
        );
        self::assertModelExists($liveOther);
        self::assertSoftDeleted($deletedOther);
    }

This fails because $affectedRows is actually 2, and if you comment out that assertion, the last assertion fails because $deletedOther has been forceDeleted. Even weirder, obviously onlyTrashed by itself isn't the problem, that count works, and if you get() the collection and do each->forceDelete() there's no problem.

Using single-table-inheritance v1.0.0 with Laravel framework 9.36.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions