Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHPUnit in Laravel fails with could not find driver on PRAGMA foreign_keys

Errorcould not find driver (SQL: PRAGMA foreign_keys = ON;)

Short answer

The PRAGMA query is only the first thing Laravel sends to a SQLite connection. The error means the PHP that runs your tests has no pdo_sqlite driver. Check which drivers it lists, enable pdo_sqlite in its php.ini, and on Windows make sure the SQLite library is on the PATH.

  • Applies to PHP 8.3, Laravel 12, PHPUnit 11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the PHP you run tests with which PDO drivers it has. sqlite must be in the list.

    php -r 'print_r(PDO::getAvailableDrivers());'
  2. 2Match what you see, then jump to its fix

    1. 1The list from the first step has no sqlite entry.pdo_sqlite is not loaded in the command-line PHPFix
    2. 2The extension line is enabled in php.ini but pdo_sqlite still does not load.On Windows the SQLite library cannot be foundFix
    3. 3The extension line is missing or fails, and the PHP binary is a custom or minimal build.PHP was built without PDO_SQLITEFix

What does the error look like?

  • could not find driver (SQL: PRAGMA foreign_keys = ON;)

Cause 1 of 3

pdo_sqlite is not loaded in the command-line PHP

1Check

PDO_SQLITE is enabled by default in a standard build. A package or shared build can leave it unloaded, and the command line may read a different php.ini than your web server.

php --ini
php -m | grep -i sqlite

2Fix

Enable the extension in the ini file that php --ini shows as loaded.

; php.ini
extension=pdo_sqlite

3Verify

The driver list now contains sqlite.

php -r 'var_dump(extension_loaded("pdo_sqlite"));'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

On Windows the SQLite library cannot be found

1Check

The PHP manual says libsqlite3.dll must be on the system PATH on Windows, and that adding the PHP directory to PATH is the documented way.

where php

2Fix

Add the folder that contains php.exe and its DLLs to the PATH, then open a new terminal.

# Windows: add the PHP folder to PATH in system environment variables, reopen the terminal

3Verify

Check the driver list again.

php -r "print_r(PDO::getAvailableDrivers());"

Confirmed by php.net

Did this fix it?

Cause 3 of 3

PHP was built without PDO_SQLITE

1Check

The manual describes the driver as enabled by default, and gives the configure switch --without-pdo-sqlite for builds that leave it out.

php -i | grep -i "pdo_sqlite\|configure command"

2Fix

Use a PHP build that includes PDO_SQLITE, or rebuild without the --without-pdo-sqlite switch.

# rebuild PHP without: --without-pdo-sqlite

3Verify

Check the driver list again.

php -m | grep -i sqlite

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run php --ini in CI and locally; both must load the same extensions your tests need.
  • Keep the SQLite driver in your PHP install notes for a new machine.
  • Remember that Laravel enables SQLite foreign key constraints by default, so the PRAGMA is the first query on every SQLite connection.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Laravel says the SQLite database file does not exist

SQLiteDatabaseDoesNotExistException

Laravel checks the SQLite file before it connects, and it throws SQLiteDatabaseDoesNotExistException when it cannot resolve the path in DB_DATABASE.

3 causes, each with a fix

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.