Laravel truncating table causing error during test

Viewed 1463

I'm trying to truncate a table, but everytime I try to run it through a test I get a PDOException: There is no active transaction

I'm using the RefreshDatabase trait.

My code looks like this:

Model::query()->truncate();
2 Answers

the error is caused by the truncate action

based on the answer on https://stackoverflow.com/a/1522974/6644975

when you execute a truncate statement the transaction is committed and then the TRUNCATE is executed and cannot be undone.

This affects the RefreshDatabase trait which uses a transaction for each and every test.

here's how it runs

  1. RefreshDatabase creates a transactions
  2. You do something here with the database
  3. you execute truncate
  4. Truncate commits the transaction
  5. RefreshDatabase tries to commit a transaction but will not be able to because it's already committed causing an error There is no active transaction

Try to use the trait DatabaseMigrations instead of RefreshDatabase

Related