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

fub's avatar
Level 4

SoftDeletes added to existing table will make my previous test fail

Hi, I have an Laravel 8 app which is already in production. Now I decided to add soft deletes to a specific model. I added the trait and created a migration. Using a artisan migrate works and the soft deletes are used then - fine!

The problem is with my tests. Suddenly all my tests fail with:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bookings.deleted_at' in 'where clause' (SQL: select * from `bookings` where `bookings`.`deleted_at` is null)

When I look at the test database after all my tests failed, I can see that the newly created migration was not run - there is no deleted_at field.

My guess is now that when running the first/old tests, Laravel tries to find all needed migrations (and does not run all migrations). It misses the last one (maybe because its on a related model?) and this will make my tests fail because of the SoftDeletes traites, a deleted_at column is expected.

Any ideas what I can do to fix this no?

0 likes
6 replies
maclervil's avatar

In your test class, you can use the trait RefreshDatabase. Every time use execute your test, it will refresh your migration.

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}
fub's avatar
Level 4

Yes, I am doing this already in every test class.

maclervil's avatar

Like that ??

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}
alanholmes's avatar

I have just had the same issue, and the reason was that between the initial table migration and the migration to add the soft deletes, I had another migration, that was adding a column, and then running queries to populate that column.

This was then adding the soft delete scope, before the deleted_at column is created.

So to get around this, in the affected migration, I added withoutGlobalScope(new SoftDeletingScope()) to prevent that earlier migration trying to add the soft-deleted scope.

4 likes

Please or to participate in this conversation.