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

mikebronner's avatar

sqLite "no such table" Error In Unit Tests With Migrations

I have set up unit tests in Laravel 5.1 using sqLite ion-memory databases. I am getting the following error when running my tests:

PDOException: SQLSTATE[HY000]: General error: 1 no such table: versions

My unit test employ migration and transaction traits as per the Laravel documentation: http://laravel.com/docs/5.1/testing

The stack trace shows that this error occurs during the roll-back phase after each test is performed. I did a quick select statement to verify the table exists at the end of each test.

The only way I have been able to get around this is to update the migration file to the following:

        if (Schema::hasTable('versions')) {
            Schema::drop('versions');
        }

Any idea what is happening here?

Thanks!

0 likes
7 replies
derekmcwhinnie's avatar

I have (hopefully had) a very similar problem with a table not found error when running phpunit. The test worked fine when using my development database (local.sqlite) but failed when I used the phpunit configuration (phpunit.sqlite). Simply trying to have a separate DB for the unit tests.

After alot of head scratching and stepping through the code it appears that the database to be used for tests must be migrated manually once after creation to initialise it BEFORE the tests are run. The DatabaseMigrations trait appears to expect a base migration to exist already before it can roll it back. It does not appear to like a completely empty sqlite file (as created with "touch abc.sqlite" etc.

I set up a config.database with this entry

'phpunit' => [
            'driver'   => 'sqlite',
            'database' => storage_path('phpunit.sqlite'),
            'prefix'   => '',
        ],

the phpunit.xml file had these entries

  <env name="DB_FILE" value="phpunit.sqlite"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_HOST" value="localhost"/>

then I ran

php artisan migrate --database='phpunit'

After this phpunit stopped complaining and all test started to behave.

2 likes
arindam's avatar

Please look if you are calling Artisan::call('migrate'); before running each test

1 like
ejdelmonico's avatar

I use this in phpunit.xml:

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

Then, in the test class, use DatabaseMigrations; at the top of the class. By doing this, it creates the tables and destroys the tables during a test run.

1 like
Nospoon's avatar

I'm having the same problem with the new project I started recently. Funny thing is, I have this working perfectly in another project, but can't get it working on the new one.

Grumbleman's avatar

happened to me when i did php artisan config:clear afterwards i needed to put use DatabaseMigrations; at the top of each test class, like @ejdelmonico said.

ssa4141's avatar

i solved this problem using by "use RefreshDatabase;" in Test file.

kortby's avatar

Check if you might have mentioned versions table on validation or somewhere wrong

$request->validate([
            'title' => 'required|unique:versions', //. <----like this
        ]);

Please or to participate in this conversation.