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

Ezrab_'s avatar

Laravel testing DB There is no active transaction

I'm trying to update my model with a test to see if it works correctly. The way I'm updating is probably not best practise, but I couldn't figure out a better way so what I did was:

$this->openingTime->truncate();
$this->openingTime->insert($openingTimes);

This works fine for actually inserting (updating) the data. But when I try to test it I get back:

PDOException
There is no active transaction

So I tried adding DB::beginTransaction(); before the truncating and inserting, but then I get the error:

Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist

My test looks like this:

$response = $this->put('/api/opening-times', [
    'openingTimes' => [
        [
            'opening_time' => null,
            'closing_time' => null,
        ],
    ]
]);
$response->assertStatus(Response::HTTP_NO_CONTENT);
0 likes
4 replies
Ezrab_'s avatar

Now receiving SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist

public function test_update_opening_times()
    {
        $this->beginDatabaseTransaction();

        $response = $this->put('/api/opening-times', [
            'openingTimes' => [
                [
                    'opening_time' => null,
                    'closing_time' => null,
                ],
            ]
        ]);

        $response->assertStatus(Response::HTTP_NO_CONTENT);
    }
theo's avatar

I know this is an old post, but I've run into it myself and figure it would be good to have the answer posted somewhere.

The issue is that "truncate" is a command that generates an "implicit commit" (https://mariadb.com/kb/en/sql-statements-that-cause-an-implicit-commit/). Therefore the truncate command closes open transactions so that when Laravel goes to commit the transaction it opened, the transaction is gone, already been closed by the implicit commit.

1 like

Please or to participate in this conversation.