Skip to content
Astera Technology

Why did a half-filled composite reference pass its foreign key?

Enforce the supplied all-or-none link rule alongside the composite FK.

Download complete case notes and answer keys ↓

Read the source and its declared rule

The original optional link is either both site, parentId absent or both supplied and matched. In SQLite a NULL component exempts the composite FK match, so North/null can pass the FK while violating this all-or-none rule. Corrected CHECK requires both NULL or both non-NULL.

Filled synthetic input
RecordInput JSON
Base input{"site":"North","parentId":null}
Download editable CSV ↓

Compare the actual paths

Corrected partial link fails 275 and creates no child. Both-null remains a legitimate no-link record rather than inventing a parent.

Misleading and corrected outputs
PathOutput JSON
Misleading path{"error":null,"children":[{"site":"North","parent_id":null}]}
Corrected path{"error":{"code":275},"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=ON;CREATE TABLE parent(site TEXT,id INTEGER,PRIMARY KEY(site,id));INSERT INTO parent VALUES('North',1);CREATE TABLE child(site TEXT,parent_id INTEGER,FOREIGN KEY(site,parent_id) REFERENCES parent(site,id)"+(corrected?',CHECK((site IS NULL AND parent_id IS NULL) OR(site IS NOT NULL AND parent_id IS NOT NULL))':'')+');');
      const error=failed(()=>db.prepare('INSERT INTO child VALUES(?,?)').run(i.site,i.parentId));return {error,children:rows(db,'SELECT * FROM child')};
    }finally{db.close();}
  };
const cases=[{"label":"misleading base","input":{"site":"North","parentId":null},"expected":{"error":null,"children":[{"site":"North","parent_id":null}]},"corrected":false},{"label":"corrected base","input":{"site":"North","parentId":null},"expected":{"error":{"code":275},"children":[]},"corrected":true},{"label":"legitimate entirely absent link","input":{"site":null,"parentId":null},"expected":{"error":null,"children":[{"site":null,"parent_id":null}]},"corrected":true},{"label":"complete matching pair","input":{"site":"North","parentId":1},"expected":{"error":null,"children":[{"site":"North","parent_id":1}]},"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

Both-null is accepted. Complete North/1 matches the real parent and is accepted.

Editable boundary records and expected outputs
CaseInput JSONExpected output JSON
Entirely absent optional link{"site":null,"parentId":null}{"error":null,"children":[{"site":null,"parent_id":null}]}
Complete matched pair{"site":"North","parentId":1}{"error":null,"children":[{"site":"North","parent_id":1}]}
Download editable CSV ↓

Does FK acceptance prove both reference components were completed?

Show the worked answer

No. North/null bypasses the composite match. Enforce the original all-or-none rule separately.

Related resources