Read the source and its declared rule
This original target derives area=width×height as a stored generated column. Width and height are required small whole dimensions in the supplied cases. SourceArea 99 is retained in the input for comparison, but it is not an authorised writable area override.
| Record | Input JSON |
|---|---|
| Base input | {"width":2,"height":3,"sourceArea":99} |
Compare the actual paths
The bad ordinary INSERT into area fails. Correct base insertion stores 2×3=6. A missing width still fails NOT NULL; omitting the generated field does not waive its inputs.
| Path | Output JSON |
|---|---|
| Misleading path | {"error":{"code":1},"records":[]} |
| Corrected path | {"error":null,"records":[{"width":2,"height":3,"area":6}]} |
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('CREATE TABLE target(width INTEGER NOT NULL,height INTEGER NOT NULL,area INTEGER GENERATED ALWAYS AS(width*height) STORED);');
const error=failed(()=>corrected?db.prepare('INSERT INTO target(width,height) VALUES(?,?)').run(i.width,i.height):db.prepare('INSERT INTO target(width,height,area) VALUES(?,?,?)').run(i.width,i.height,i.sourceArea));
return {error,records:rows(db,'SELECT * FROM target')};
}finally{db.close();}
};
const cases=[{"label":"misleading base","input":{"width":2,"height":3,"sourceArea":99},"expected":{"error":{"code":1},"records":[]},"corrected":false},{"label":"corrected base","input":{"width":2,"height":3,"sourceArea":99},"expected":{"error":null,"records":[{"width":2,"height":3,"area":6}]},"corrected":true},{"label":"changed base dimensions","input":{"width":4,"height":3,"sourceArea":99},"expected":{"error":null,"records":[{"width":4,"height":3,"area":12}]},"corrected":true},{"label":"missing required base","input":{"width":null,"height":3,"sourceArea":99},"expected":{"error":{"code":1299},"records":[]},"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);
}
Check boundaries before the next load
Changing width to 4 derives 12. Missing width creates no target row.
| Case | Input JSON | Expected output JSON |
|---|---|---|
| Changed base width | {"width":4,"height":3,"sourceArea":99} | {"error":null,"records":[{"width":4,"height":3,"area":12}]} |
| Missing required dimension | {"width":null,"height":3,"sourceArea":99} | {"error":{"code":1299},"records":[]} |
Should sourceArea 99 be written over derived 6?
Show the worked answer
No. This schema owns the calculation. Insert width 2, height 3 and read area 6; a different override model needs a separate agreed schema.