I use PDO for connecting to sqlite3, but I cannot make foreign keys working for some reason. According to docs, this “PRAGMA short_column_names=1” should enable that. I do this:
$con = new PDO('sqlite:z:/testing.db');
$res = $con->exec('PRAGMA foreign_keys=ON');
var_dump($res);die();
Which returns me 0. I tried creating actual tables with foreign keys and it did not work.
Direct requests to SQLite3 class worked though:
$con = new SQLite3('z:/testing.db');
$con->exec('PRAGMA foreign_keys = ON;');
var_dump($con->query('PRAGMA foreign_keys;')->fetchArray());
This reutrns array(2) { [0]=> int(1) [“foreign_keys”]=> int(1) }
According to SQLite3::version(), I have sqlite version 3.7.7.1. My PHP version is 5.3.18, running on Windows.
Please help me get it running with PDO. Thanks!
I’m not aware of the idea that
PRAGMA short_column_names=1;should be issued with PDO in order for foreign key constraints to work with SQLite. You should however issuePRAGMA foreign_keys = ON;with PDO as well.Give this a try:
It should result in the following: