Skip to content
Astera Technology

Why did enabling foreign keys fail to protect this import?

Configure and inspect the connection setting before beginning the load transaction.

Download complete case notes and answer keys ↓

Read the source and its declared rule

The original fixture explicitly starts foreign_keys OFF, regardless of the Node connection default. Changing it to ON after BEGIN has no effect inside that transaction. The required import path sets ON before BEGIN, verifies the setting and treats an unmatched parent as a failed child write.

Filled synthetic input
RecordInput JSON
Base input{"parentPresent":false,"parentId":1}
Download editable CSV ↓

Compare the actual paths

Bad output still reports enabled 0 and stores the orphan. Correct output reports 1, actual FK code 787 and no committed child because the transaction is rolled back.

Misleading and corrected outputs
PathOutput JSON
Misleading path{"enabled":0,"error":null,"children":[{"id":1,"parent_id":1}]}
Corrected path{"enabled":1,"error":{"code":787},"children":[]}
Download editable CSV ↓
Local Node.js program: 22.23.3 or 24.14.1
import {DatabaseSync} from 'node:sqlite';
import {TextDecoder} from 'node:util';
import {inflateSync} from 'node:zlib';
import {posix} from 'node:path';
import {createHash} from 'node:crypto';
const rows=(db,sql)=>db.prepare(sql).all().map(r=>({...r}));
const failed=(action)=>{try{action();return null;}catch(e){return {code:e.errcode??e.code??e.name};}};

import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
    const db=new DatabaseSync(':memory:');try{db.exec('PRAGMA foreign_keys=OFF;CREATE TABLE parent(id INTEGER PRIMARY KEY);CREATE TABLE child(id INTEGER PRIMARY KEY,parent_id INTEGER REFERENCES parent(id));');
      if(i.parentPresent)db.exec('INSERT INTO parent VALUES(1);');
      if(corrected){db.exec('PRAGMA foreign_keys=ON;');if(db.prepare('PRAGMA foreign_keys').get().foreign_keys!==1)throw new Error('Required enforcement unavailable');}db.exec('BEGIN;');
      if(!corrected)db.exec('PRAGMA foreign_keys=ON;');
      const error=failed(()=>db.prepare('INSERT INTO child VALUES(1,?)').run(i.parentId));
      db.exec(error?'ROLLBACK':'COMMIT');
      return {enabled:db.prepare('PRAGMA foreign_keys').get().foreign_keys,error,children:rows(db,'SELECT * FROM child')};
    }finally{db.close();}
  };
const cases=[{"label":"misleading base","input":{"parentPresent":false,"parentId":1},"expected":{"enabled":0,"error":null,"children":[{"id":1,"parent_id":1}]},"corrected":false},{"label":"corrected base","input":{"parentPresent":false,"parentId":1},"expected":{"enabled":1,"error":{"code":787},"children":[]},"corrected":true},{"label":"existing parent permits child","input":{"parentPresent":true,"parentId":1},"expected":{"enabled":1,"error":null,"children":[{"id":1,"parent_id":1}]},"corrected":true},{"label":"explicitly optional parent","input":{"parentPresent":false,"parentId":null},"expected":{"enabled":1,"error":null,"children":[{"id":1,"parent_id":null}]},"corrected":true}];
for(const test of cases){
  const actual=run(test.input,test.corrected);
  assert.deepEqual(actual,test.expected,test.label);
  console.log(test.label,actual);
}
Download runnable example ↓

Check boundaries before the next load

A matching parent allows the child. This schema’s explicitly optional null parent is also allowed.

Editable boundary records and expected outputs
CaseInput JSONExpected output JSON
Matching parent exists{"parentPresent":true,"parentId":1}{"enabled":1,"error":null,"children":[{"id":1,"parent_id":1}]}
Permitted optional link{"parentPresent":false,"parentId":null}{"enabled":1,"error":null,"children":[{"id":1,"parent_id":null}]}
Download editable CSV ↓

Does issuing PRAGMA ON after BEGIN prove enforcement is active?

Show the worked answer

No. The bad connection remains OFF. Set and inspect ON before the transaction; the missing-parent write then fails.

Related resources