Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:05:52+00:00 2026-06-14T19:05:52+00:00

I use PDO for connecting to sqlite3, but I cannot make foreign keys working

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:05:52+00:00Added an answer on June 14, 2026 at 7:05 pm

    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 issue PRAGMA foreign_keys = ON; with PDO as well.

    Give this a try:

    <?php
    
    // SQL for creating database structure
    $databaseSql = <<<SQL
        CREATE TABLE `user` (
            `id` INTEGER PRIMARY KEY AUTOINCREMENT,
            `name` TEXT NOT NULL,
            UNIQUE( `name` )
        );
    
        CREATE TABLE `userProfile` (
            `userId` INTEGER NOT NULL CONSTRAINT `userProfile_userId` REFERENCES `user`( `id` ) ON UPDATE CASCADE ON DELETE CASCADE,
            `image` TEXT NOT NULL
        );
    SQL;
    
    // SQL for inserting dummy data
    $dataSql = <<<SQL
        INSERT INTO `user` VALUES( 1, "John" );
        INSERT INTO `user` VALUES( 2, "Mary" );
        INSERT INTO `user` VALUES( 3, "Joe" );
        INSERT INTO `userProfile` VALUES( 1, "/images/john.jpg" );
        INSERT INTO `userProfile` VALUES( 2, "/images/mary.jpg" );
        INSERT INTO `userProfile` VALUES( 3, "/images/joe.jpg" );
    SQL;
    
    // create a temporary SQLite instance in memory
    $db = new PDO( 'sqlite::memory:', null, null, array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false
    ) );
    
    // activate use of foreign key constraints
    $db->exec( 'PRAGMA foreign_keys = ON;' );
    
    // create database
    $db->exec( $databaseSql );
    // insert dummy data
    $db->exec( $dataSql );
    
    // should dump 3 records
    var_dump( $db->query( 'SELECT * FROM `userProfile`;' )->fetchAll() );
    
    // delete 1 user, cascade deleting 1 userProfile as well
    $db->exec( 'DELETE FROM `user` WHERE `id` = 1;' );
    
    // should dump 2 records
    var_dump( $db->query( 'SELECT * FROM `userProfile`;' )->fetchAll() );
    

    It should result in the following:

    array(3) {
      [0]=>
      array(2) {
        ["userId"]=>
        string(1) "1"
        ["image"]=>
        string(16) "/images/john.jpg"
      }
      [1]=>
      array(2) {
        ["userId"]=>
        string(1) "2"
        ["image"]=>
        string(16) "/images/mary.jpg"
      }
      [2]=>
      array(2) {
        ["userId"]=>
        string(1) "3"
        ["image"]=>
        string(15) "/images/joe.jpg"
      }
    }
    array(2) {
      [0]=>
      array(2) {
        ["userId"]=>
        string(1) "2"
        ["image"]=>
        string(16) "/images/mary.jpg"
      }
      [1]=>
      array(2) {
        ["userId"]=>
        string(1) "3"
        ["image"]=>
        string(15) "/images/joe.jpg"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reworking some PHP code to use PDO for the database access, but I'm
I am re-working all of my mysql_* and mysqli connections to use PDO but
I want to use pdo in my application, but before that I want to
If I use PDO and use FETCH_INTO like ... $query->setFetchMode(PDO::FETCH_INTO, $this); $query->execute(); I have
I'm trying to use PDO in this way and reuse the output template: selected_products_show
I am still working with securing my web app. I decided to use PDO
I'm trying to use the PDO MySQL driver in my CodeIgniter application. This is
starting use oop why: class user { private $pdo; function __construct() { $this->pdo =
I'm trying to use a simple UPDATE query with pdo but after 5 hours
I am using php PDO and I have this error when I use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.