PHPUnit in Laravel fails with could not find driver on PRAGMA foreign_keys
could 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.
Fix it fast
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());'2Match what you see, then jump to its fix
- 1
The list from the first step has no sqlite entry.pdo_sqlite is not loaded in the command-line PHPFix - 2
The extension line is enabled in php.ini but pdo_sqlite still does not load.On Windows the SQLite library cannot be foundFix - 3
The extension line is missing or fails, and the PHP binary is a custom or minimal build.PHP was built without PDO_SQLITEFix
- 1
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 sqlite2Fix
Enable the extension in the ini file that php --ini shows as loaded.
; php.ini
extension=pdo_sqlite3Verify
The driver list now contains sqlite.
php -r 'var_dump(extension_loaded("pdo_sqlite"));'Confirmed by php.net
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 php2Fix
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 terminal3Verify
Check the driver list again.
php -r "print_r(PDO::getAvailableDrivers());"Confirmed by php.net
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-sqlite3Verify
Check the driver list again.
php -m | grep -i sqliteConfirmed by php.net
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:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- DCORAn offline-first field app with three roles, GPS attendance, a dynamic form engine and five languages.
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 fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack 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 fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack 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 fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fix