fix: placeholder spacing and unsatisfiable constraints during introspection#11
Merged
uwemaurer merged 1 commit intoJul 24, 2026
Conversation
…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
deleted the
fix/issue-10-placeholder-spacing-and-fk-introspection
branch
July 24, 2026 18:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #10 — two gotchas reported by @mathijs81.
LIMIT ${param}generatedLIMIT$1Postgres lexes
LIMIT$1as the identifierlimit$1and 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 joinedsqlstring, leaving it inconsistent withsqlParts(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/EXECis introspected in its own transaction, so anINSERTwith a NOT NULL foreign key can never be satisfied — the reporter worked around it withDEFERRABLE INITIALLY DEFERRED.After initialization, referential integrity is no longer enforced:
PRAGMA foreign_keys = OFF(better-sqlite3 turns it on by default)session_replication_role = replica— best effort, needs superuser;SET LOCALbehind a savepoint in external-url:modeEXECwarns instead of failingTolerating an
EXECfailure is safe because nothing in the generated code depends on it running — parameter types come from the prepared statement. AQUERY(includingINSERT ... 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@setliterals (@set author_id = 42demands an author withid = 42) and gets unworkable with self-referencing, composite, or multi-level FK chains.Tests
placeholder-spacing.test.ts— spacing across all three renderings,sqlParts/sqlconsistency, no spurious space after operatorsforeign-keys.test.ts— SQLite and DuckDB generate despite an unsatisfiable FK, and a FK violation in TESTDATA still throwssqltool-pg.test.ts— same for Postgres against the testcontainer22 snapshots updated;
git diff -wover them is empty, i.e. whitespace restoration only (priority =$1→priority = $1,VALUES (?,?,?)→VALUES (?, ?, ?)).Verified: vitest 210 passed,
just test-javaBUILD SUCCESSFUL,just test-python54 passed,pnpm buildclean, biome unchanged from baseline (13 warnings / 2 infos before and after).🤖 Generated with Claude Code