Skip to content

fix: placeholder spacing and unsatisfiable constraints during introspection#11

Merged
uwemaurer merged 1 commit into
mainfrom
fix/issue-10-placeholder-spacing-and-fk-introspection
Jul 24, 2026
Merged

fix: placeholder spacing and unsatisfiable constraints during introspection#11
uwemaurer merged 1 commit into
mainfrom
fix/issue-10-placeholder-spacing-and-fk-introspection

Conversation

@uwemaurer

Copy link
Copy Markdown
Contributor

Fixes #10 — two gotchas reported by @mathijs81.

LIMIT ${param} generated LIMIT$1

Postgres lexes LIMIT$1 as the identifier limit$1 and rejects the query.

Root cause: when the parser reached a ${var} node it sliced the preceding SQL up to the end of the last token instead of up to the start of the variable, dropping the whitespace in front of the placeholder. Whitespace after a variable was kept — that asymmetry is why only the leading side broke.

Placeholders now go through pushPlaceholder(), which separates a placeholder from a preceding word character in all three rendering paths. This replaces the old fix-up that patched only the joined sql string, leaving it inconsistent with sqlParts (what the TS/Java template-literal renderers emit).

One existing snapshot already contained select 1 as n where$1 ::int is null, so the bug was baked into the test corpus.

Unsatisfiable foreign keys during introspection

Each QUERY/EXEC is introspected in its own transaction, so an INSERT with a NOT NULL foreign key can never be satisfied — the reporter worked around it with DEFERRABLE INITIALLY DEFERRED.

After initialization, referential integrity is no longer enforced:

Engine Mechanism
SQLite PRAGMA foreign_keys = OFF (better-sqlite3 turns it on by default)
PostgreSQL session_replication_role = replica — best effort, needs superuser; SET LOCAL behind a savepoint in external-url: mode
DuckDB no such switch, so a constraint violation in an EXEC warns instead of failing

Tolerating an EXEC failure is safe because nothing in the generated code depends on it running — parameter types come from the prepared statement. A QUERY (including INSERT ... RETURNING) still executes and still fails, since its result shape is what gets introspected.

Fixtures stay checked. The relaxation starts after BASELINE/MIGRATE/TESTDATA, so a foreign key violated by a fixture is still an error — that data is real and a broken reference in it is worth reporting. The alternative, requiring TESTDATA to satisfy every EXEC, would couple fixture primary keys to each query's @set literals (@set author_id = 42 demands an author with id = 42) and gets unworkable with self-referencing, composite, or multi-level FK chains.

Tests

  • placeholder-spacing.test.ts — spacing across all three renderings, sqlParts/sql consistency, no spurious space after operators
  • foreign-keys.test.ts — SQLite and DuckDB generate despite an unsatisfiable FK, and a FK violation in TESTDATA still throws
  • sqltool-pg.test.ts — same for Postgres against the testcontainer

22 snapshots updated; git diff -w over them is empty, i.e. whitespace restoration only (priority =$1priority = $1, VALUES (?,?,?)VALUES (?, ?, ?)).

Verified: vitest 210 passed, just test-java BUILD SUCCESSFUL, just test-python 54 passed, pnpm build clean, biome unchanged from baseline (13 warnings / 2 infos before and after).

🤖 Generated with Claude Code

…ection

Both reported in #10.

`LIMIT ${n}` rendered as `LIMIT$1`, which Postgres lexes as an identifier.
When the parser reached a `${var}` node it sliced the preceding SQL up to the
end of the last token instead of up to the start of the variable, dropping the
whitespace in front of the placeholder. Whitespace after a variable was kept,
which is why only the leading side broke.

Placeholders are now appended through pushPlaceholder(), which separates them
from a preceding word character in every rendering path — so the joined `sql`
string and `sqlParts` (used by the template-literal renderers) can no longer
disagree, as they did under the old fix-up that only patched `sql`.

Introspection also runs each QUERY and EXEC in its own transaction, so a NOT
NULL foreign key could never be satisfied. Once MIGRATE and TESTDATA have run,
referential integrity is no longer enforced: PRAGMA foreign_keys = OFF for
SQLite, session_replication_role = replica for Postgres (best effort, it needs
superuser). DuckDB has no such switch, so a constraint violation in an EXEC is
warned about instead of fatal — nothing in the generated code depends on an
EXEC running, its parameter types come from the prepared statement.

Fixtures stay honest: MIGRATE and TESTDATA run before the constraints are
relaxed, so a foreign key they violate is still an error.

Snapshot churn is whitespace only (`git diff -w` is empty).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@uwemaurer
uwemaurer merged commit 97993fa into main Jul 24, 2026
1 check passed
@uwemaurer
uwemaurer deleted the fix/issue-10-placeholder-spacing-and-fk-introspection branch July 24, 2026 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issues discovered by my coding agent

1 participant