laravel and phpunit: could not find driver (SQL: PRAGMA foreign_keys = ON;)
Asked Answered
O

13

36

I have run my laravel app with phpunit. Everything is fine until at some point when I run my test again turns out with this error.

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)

enter image description here

Caused by
PDOException: could not find driver

Here's my phpunit.xml file:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
     colors="true">
<testsuites>
    <testsuite name="Unit">
        <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
        <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
</testsuites>
<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
    </whitelist>
</filter>
<php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="sqlite"/>
    <server name="DB_DATABASE" value=":memory:"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
</php>
OS: Windows 10 | php version: PHP 7.4.11 | sqlite version: SQLite version 3.19.1

Does anyone encounter with this error in laravel 6 and phpunit?

Overcast answered 15/10, 2020 at 23:58 Comment(4)
can you please post more information? Like your phpunit.xml file. Are you using sqlite?Blunger
Yes I'm using sqlite and database value as memory. Please refer my updated post @JonathanMartinsOvercast
Whats you OS? Whats the PHP version? do you have the sqlite driver installed?Blunger
Yup I have sqlite installed. Updated my post as per your questions.Overcast
O
30

This is actually happening after I upgraded my php version in laragon. The solution is to enable pdo_sqlite on php extension in laragon. That's it. This answer found here https://laracasts.com/discuss/channels/general-discussion/phpunit-sqlite-error-after-updating-laragon-pdoexception-could-not-find-driver

Overcast answered 17/10, 2020 at 0:32 Comment(3)
Glad that you find the solution to your problem.Blunger
Yup happy to find the solutionOvercast
It worked. Do not forget to stop and start all servicesMogilev
P
85

If You face this problem

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

You can simply run bellow command on your Ubuntu system

sudo apt-get install php-sqlite3

Also if you want to install specific version like php 8.1 simply run bellow command

sudo apt install php8.1-sqlite3

You might need to enable pdo_sqlite extension in your php.ini too, you can get the path of your loaded ini with the below command:

php --ini

simply add extension=pdo_sqlite to your php.ini and verify it is loaded by running

php -m | grep pdo_sqlite

I think it will help you.. Thanks

Publicness answered 11/11, 2021 at 5:26 Comment(2)
For php8.1 the package is php8.1-sqlite3.Collimore
Editing php.ini solved my issueEileen
O
30

This is actually happening after I upgraded my php version in laragon. The solution is to enable pdo_sqlite on php extension in laragon. That's it. This answer found here https://laracasts.com/discuss/channels/general-discussion/phpunit-sqlite-error-after-updating-laragon-pdoexception-could-not-find-driver

Overcast answered 17/10, 2020 at 0:32 Comment(3)
Glad that you find the solution to your problem.Blunger
Yup happy to find the solutionOvercast
It worked. Do not forget to stop and start all servicesMogilev
M
10

PHP 8.1

sudo apt install php8.1-sqlite3

Note that it needs the 8.1 part!

Monteverdi answered 30/1, 2022 at 0:30 Comment(0)
A
5

This happens because PDO Sqlite is not enabled. to enable it in Laragon, just open Laragon click on menu > PHP > Extensions > pdo_sqlite That's it.

And if you don't use Laragon the solution might be different. Thanks

Amphibian answered 8/11, 2021 at 15:51 Comment(0)
I
5

For those using windows;

Navigate to the php.ini file(-C:\php\php.ini)

Enable the following:

;extension=pdo_sqlite by removing the /;/ should look like this extension=pdo_sqlite

;extension=sqlite3 should be extension=sqlite3 without the ; symbol
Indianapolis answered 2/1, 2022 at 9:35 Comment(1)
Path to that ini file isn't necessarily always that C:\php\php.ini and can depend on your installation. You can run php --ini command in your terminal to get path to ini file for your current active PHP installation.Lepidote
B
5

If you come across such an error,simply do the following;

1.run this command to install sqlite driver in your ubuntu or linux OS

sudo apt-get install php-sqlite3
  1. You also need to enable pdo_sqlite extension in your php.ini too, check the path of your loaded .ini with the below command

    php --ini

3.Then check and verify that the pdo_sqlite extension is loaded.

php -m | grep pdo_sqlite

Then the error will be solved.

Boomer answered 4/2, 2023 at 11:30 Comment(0)
M
4

just enabling these two extension from php.ini file worked for me.

extension=pdo_sqlite

extension=sqlite3
Mayworm answered 24/4, 2022 at 5:38 Comment(0)
C
3

Just do the following changes on the php.ini file

extension_dir = "<php installation directory>/php-7.4.3/ext"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
sqlite3.extension_dir = "<php installation directory>/php-7.4.3/ext"
Catalyze answered 7/11, 2021 at 12:18 Comment(0)
B
3

If you use Ubuntu try this:

php --version
sudo apt-get install PHP (your php version)-sqlite3

This works for me.

https://laracasts.com/discuss/channels/laravel/sqlite-database-throw-error-could-not-find-driver-sql-pragma-foreign-keys-on

Bronchopneumonia answered 4/1, 2023 at 6:8 Comment(1)
It's very important to check if your version of PHP CLI first, php.ini might be load a different version so.. you should match phpx.y-sqlite3 packegeSurrebutter
P
1

In case if you have multiple versions of PHP in Laragon you will still see the error. The solution is to enable pdo_sqlite extension on all versions. The reason is that Laragon has its own PHP but Laravel uses the system PHP and you might not know which one is system PHP version.

Pharmacopsychosis answered 26/10, 2021 at 7:55 Comment(0)
G
0

At first uncomment ;extension=pdo_sqlite in php.ini in /etc/php/{your_php_version}/php.ini on Linux or C:\php\php.ini on Windows by removing the semicolon at the beginning.

then sudo service apache2 restart

then sudo apt-get install php-sqlite3

Gilus answered 25/2 at 19:22 Comment(0)
S
0

You need to install sqlite3 package in linux distro

sudo apt-get install php$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')-sqlite3  php-sqlite3

This command automatically install the sqlite3 package based on your current PHP version in your Linux distro.

Slightly answered 26/3 at 6:25 Comment(0)
E
0

Another Suggestion, if you see this error after installing Laravel 11, and you're not using SQLite, you must know that Laravel 11 par default using SQLite to manage sessions, to fix this just change the environment variable like this :

...
SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null
...

to

...
SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null
...

hope this helps someone.

Essonite answered 27/4 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.