Skip to content

Commit 3ff6cb9

Browse files
ryanrasticlaude
andcommitted
refactor(insert): allowlist DEFAULT keyword to pg; other dialects throw
Invert the heterogeneous-rows branch: instead of special-casing sqlite to throw and assuming every other dialect accepts the DEFAULT keyword, only postgres (known to support it) gets DEFAULT and any other dialect raises. A future dialect fails loud on first use instead of emitting SQL that may parse as something else. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 1474b75 commit 3ff6cb9

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

src/builder/insert.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,20 @@ export class FinalizedInsert<Name extends string, T extends TableBase, R extends
4848
const vals = usedColumns.map((k) => {
4949
const v = row[k];
5050
if (v === undefined) {
51-
// Some other row provides this column, this one doesn't. PG
52-
// spells that `DEFAULT`; SQLite has no per-row spelling for
53-
// it, and silently inserting NULL would diverge from what
54-
// omitting the column means (rowid auto-fill, declared
55-
// DEFAULT). Make the caller decide.
56-
if (tableCls.database.dialect === "sqlite") {
57-
throw new Error(
58-
`Insert into '${tableName}': column '${k}' is set in some rows but not others. ` +
59-
`SQLite cannot express "use the column default" per row — provide '${k}' in every row or in none.`,
60-
);
51+
// Some other row provides this column, this one doesn't.
52+
// Only PG is known to spell that `DEFAULT`; for any other
53+
// dialect (SQLite has no per-row spelling for it), silently
54+
// inserting NULL would diverge from what omitting the column
55+
// means (rowid auto-fill, declared DEFAULT) — make the
56+
// caller decide.
57+
if (tableCls.database.dialect === "postgres") {
58+
return sql`DEFAULT`;
6159
}
62-
return sql`DEFAULT`;
60+
throw new Error(
61+
`Insert into '${tableName}': column '${k}' is set in some rows but not others. ` +
62+
`The '${tableCls.database.dialect}' dialect cannot express "use the column default" per row — ` +
63+
`provide '${k}' in every row or in none.`,
64+
);
6365
}
6466
const col = getColumn(instance, k);
6567
return col[meta].__class.from(v).toSql();

0 commit comments

Comments
 (0)