Skip to content
This repository was archived by the owner on Nov 25, 2026. It is now read-only.

TML-2861: emit ::jsonb / ::json cast on Postgres JSON column defaults - #763

Merged
wmadden-electric merged 5 commits into
mainfrom
tml-2861-pg-jsonb-default-cast
Jun 8, 2026
Merged

wmadden-electric merged 5 commits into
mainfrom
tml-2861-pg-jsonb-default-cast

Conversation

@wmadden-electric

@wmadden-electric wmadden-electric commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

At a glance

A Postgres jsonb column with a JSON-object default. Before this PR, the migration planner emits:

CREATE TABLE "user" (
  "metadata" jsonb NOT NULL DEFAULT '{"key":"default"}'
);

After:

CREATE TABLE "user" (
  "metadata" jsonb NOT NULL DEFAULT '{"key":"default"}'::jsonb
);

The difference is the ::jsonb cast on the default literal. Same change applies to json columns (emits ::json); non-JSON column types are unaffected.

Why it matters

Without the cast, Postgres infers the literal's type as text at parse time, then applies an implicit text → jsonb coercion at default-evaluation time (every time a row is inserted without a metadata value). Two consequences:

  1. The DDL no longer states its own intent. Reading the table definition, you can't tell whether the author meant the default to be JSON or whether they happened to write a string that looks JSON-ish. The ::jsonb cast pins the intent.
  2. Implicit coercion is fragile. It works today for jsonb/json (because Postgres special-cases them), but the same pattern breaks for types like xml or user-defined types where no implicit cast exists. Emitting the explicit cast is the form that generalises.

This is a one-line shift in emitted DDL, not a behaviour change at the table level — existing tables continue to work, the implicit coercion still fires for them. The fix is about the form of newly-emitted migration files.

The decision

The renderer that turns the DDL AST into SQL strings runs per-column-default, dispatched through a visitor. Today the visitor signature is:

interface DdlColumnDefaultVisitor<R> {
  literal(node: LiteralColumnDefault): R;
  function(node: FunctionColumnDefault): R;
}

literal receives the default value but not the column it belongs to — so it can't see that the parent column's type is jsonb and therefore can't decide to emit the cast.

The chosen fix: widen the visitor with a render-context second argument that carries the parent column's native type.

export interface DdlColumnRenderContext {
  readonly nativeType: string;
}

export interface DdlColumnDefaultVisitor<R> {
  literal(node: LiteralColumnDefault, ctx: DdlColumnRenderContext): R;
  function(node: FunctionColumnDefault, ctx: DdlColumnRenderContext): R;
}

DdlColumnDefault.accept(visitor, ctx) and both concrete subclasses (LiteralColumnDefault, FunctionColumnDefault) forward the context.

The Postgres adapter's renderColumn already has column.type in scope at the call site, so passing it through is a one-line change:

const defaultClause = column.default
  ? column.default.accept(defaultVisitor, { nativeType: column.type })
  : '';

The Postgres defaultVisitor.literal then keys off ctx.nativeType for the cast decision; the SQLite visitor accepts the context and ignores it (SQLite has no jsonb / json types).

What changed

  • relational-core/src/ast/ddl-types.ts — introduces DdlColumnRenderContext, widens the visitor interface and DdlColumnDefault.accept signature, forwards the context from both concrete *ColumnDefault.accept overrides.
  • Postgres adapter renderer (packages/3-targets/6-adapters/postgres/src/core/ddl-renderer.ts) — defaultVisitor.literal uses ctx.nativeType to decide: jsonb → append ::jsonb; json → append ::json; everything else stays cast-free. renderColumn passes the context to accept.
  • SQLite adapter renderer (packages/3-targets/6-adapters/sqlite/src/core/ddl-renderer.ts) — visitor signatures take and ignore the new context; renderColumn passes { nativeType: column.type } for symmetry.
  • E2E ddl.test.ts snapshot — metadata and tags columns now include the ::jsonb cast.

The user-facing lit(value) builder is unchanged. The pairing of value + column happens at render time, where the column type lives, rather than upstream where users hand-write lit(...) calls.

Test coverage

Four new regression-pin tests in ddl-create-table-lowering.test.ts:

  1. jsonb-column literal default emits ::jsonb.
  2. json-column literal default emits ::json.
  3. Non-JSON column types stay cast-free — covers text / int / bool / null, and an array-shaped value on a text column (the renderer keys off the column's nativeType, not the value shape).
  4. jsonb-column function defaults stay cast-free — a DEFAULT (jsonb_build_object(...)) already returns jsonb; the cast is only relevant for string-shaped JSON literals.

One existing test was strengthened: "escapes single quotes in JSON-object literal defaults" used toContain against a substring that was a prefix of the new output, so it passed silently without the cast. Now it asserts the full form with cast.

Verification

Gate Result
@prisma-next/sql-relational-core test 331 / 331
@prisma-next/target-postgres test 272 / 272
@prisma-next/adapter-postgres test 571 / 575 (4 pre-existing expected-fail; the 4 new tests count toward 571)
@prisma-next/target-sqlite test 111 / 111
@prisma-next/adapter-sqlite test 178 / 178
@prisma-next/e2e-tests framework test 105 / 105 (with the cast-restored snapshot)
pnpm lint:deps clean
pnpm fixtures:check clean (no extension-migration drift)

Scope

5 files, +127 / −28. One framework substrate file (the visitor interface); two adapter renderers; one new-tests file; one e2e snapshot.

Alternatives considered

  • Distinct AST node kinds for JSON defaults (JsonbColumnDefault, JsonColumnDefault). The visitor would gain jsonbLiteral / jsonLiteral methods. Rejected because the choice of node kind would have to be made upstream — but the natural choice point is lit(value) in user code, which has no column-type context. Pushing the decision into the planner's *Spec → DdlColumn mapping would work, but at the cost of fragmenting the literal AST surface for a dispatch the renderer is the natural home for.
  • Append the cast in renderColumn after the visitor returns, by inspecting the rendered string. Rejected because the visitor already encapsulates the "how to render a default" logic; reaching into its output from outside breaks that encapsulation, and parsing rendered SQL to decide whether to append a suffix is the kind of thing that bites you later.
  • Defer / accept the form change. The implicit coercion does fire today, so tables still work. Rejected because the cost of the fix is small, the affected surface (the visitor interface) is internal, and "the DDL states its own intent" is the kind of small correctness win that's worth taking when it's cheap. The contextless visitor would also be a recurring papercut for any future dialect feature that depends on column-type context (e.g. PostGIS, vector, enum types) — fixing it now is groundwork as much as a bug fix.

Linear

Closes TML-2861. Surfaced during PR #760's e2e snapshot review.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • PostgreSQL column defaults now emit explicit type casts (e.g., ::jsonb, ::timestamptz) for non-text-shaped column types in generated schemas. Type casts are omitted for text-shaped types and for numeric/boolean/null literal defaults.

wmadden and others added 2 commits June 8, 2026 09:49
The Postgres planner's `renderDefaultLiteral` had explicit handling for
jsonb / json column defaults: it emitted `'<json>'::jsonb` (or `::json`)
so the cast was part of the DDL, not relying on Postgres's implicit text →
jsonb coercion at default-evaluation time. The slice-4 lowering refactor
moved default rendering into `DdlColumnDefaultVisitor.literal` on the
adapter renderer — and that visitor saw only the `LiteralColumnDefault`
node, with no column-type context, so it couldn't make the cast decision.
Emitted DDL silently dropped the cast.

Postgres accepts both forms (the implicit coercion fires at default-eval
time, so tables still work), but the form change is user-visible in
emitted `migration.ts` and was an unintended slice-4 substrate gap
surfaced by the e2e `ddl.test.ts` snapshot in PR #760.

Fix:

- Widen `DdlColumnDefaultVisitor<R>` (`relational-core/ast/ddl-types.ts`)
  to take a `DdlColumnRenderContext` second arg. Today the context carries
  only the parent column's `nativeType`; the field is named so additional
  context can join without re-shaping the interface.
- `DdlColumnDefault.accept(visitor, ctx)` and both concrete subclasses'
  `accept` overrides forward the ctx to the visitor methods.
- The Postgres adapter renderer's `defaultVisitor.literal` uses
  `ctx.nativeType` to decide: jsonb → append `::jsonb`; json → append
  `::json`; everything else stays cast-free. `renderColumn` passes
  `{ nativeType: column.type }` to `accept`.
- The SQLite adapter renderer's `defaultVisitor` accepts the new ctx and
  ignores it — SQLite has no jsonb / json column type so the cast
  decision doesn't apply, but the visitor interface requires it for
  cross-dialect symmetry.
- Restore the e2e `ddl.test.ts` snapshot to include the `::jsonb` casts
  on the `metadata` and `tags` columns. The cosmetic
  `NOT NULL DEFAULT (...)` column-clause ordering (introduced by #751)
  stays — that's a separate cosmetic change with no semantic implication.

Tests:

- 4 new regression-pin tests in `ddl-create-table-lowering.test.ts`:
  `::jsonb` on jsonb-column literal defaults, `::json` on json-column
  literal defaults, no cast on non-JSON column types (text/int/bool/null/
  even array-shaped values on non-JSON columns), no cast on jsonb-column
  function defaults (a `DEFAULT (jsonb_build_object(...))` already
  returns jsonb; the cast is only needed for string-shaped JSON
  literals).
- Existing "escapes single quotes in JSON-object literal defaults"
  test strengthened to assert the full form with cast (was using
  `toContain` against a substring that happened to be a prefix of the
  new output — the test passed silently, hiding the regression).

Verification (worktree-local):

- `@prisma-next/sql-relational-core` test: 331/331.
- `@prisma-next/target-postgres` test: 272/272.
- `@prisma-next/adapter-postgres` test: 571/575 (4 expected-fail; the 4
  new tests count toward the 571).
- `@prisma-next/target-sqlite` test: 111/111.
- `@prisma-next/adapter-sqlite` test: 178/178.
- `@prisma-next/e2e-tests` framework test: 105/105 (with the updated
  snapshot — casts restored).
- `pnpm lint:deps`: clean.
- `pnpm fixtures:check`: clean (no extension-migration drift).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…t-cast

Signed-off-by: Will Madden <[email protected]>

# Conflicts:
#	test/e2e/framework/test/ddl.test.ts
@wmadden-electric
wmadden-electric requested a review from a team as a code owner June 8, 2026 07:50
@coderabbitai

coderabbitai Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@wmadden-electric, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 25 minutes and 21 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 6e9abf49-f036-41ff-846a-9974c99cee75

📥 Commits

Reviewing files that changed from the base of the PR and between c32732d and 3f48bbe.

📒 Files selected for processing (1)
  • packages/2-sql/4-lanes/relational-core/test/contract-free/column.test.ts
📝 Walkthrough

Walkthrough

The PR extends the column default visitor pattern to thread render-time context (native type information) through the visitor chain. PostgreSQL uses this context to conditionally emit explicit type casts for quoted-literal defaults on non-text-like columns, while SQLite adapts to the new interface. E2E tests confirm the new DDL output includes appropriate type casts.

Changes

Column Default Visitor Context and Type Casting

Layer / File(s) Summary
Visitor contract and context definition
packages/2-sql/4-lanes/relational-core/src/ast/ddl-types.ts
New DdlColumnRenderContext interface carries native type. Visitor methods and accept signatures are updated to accept and forward this context parameter.
PostgreSQL default casting logic
packages/3-targets/6-adapters/postgres/src/core/ddl-renderer.ts
PostgreSQL renderer emits explicit type casts (::<type>) for quoted-literal defaults on non-text-like columns using isTextLikeNativeType helper; renderColumn passes native type context to the visitor.
PostgreSQL default casting test coverage
packages/3-targets/6-adapters/postgres/test/ddl-create-table-lowering.test.ts
Tests validate JSONB defaults get ::jsonb casts, string literals to non-text types get appropriate casts, text-like columns omit casts, and numeric/boolean/null/function defaults omit casts.
SQLite adapter context implementation
packages/3-targets/6-adapters/sqlite/src/core/ddl-renderer.ts
SQLite visitor methods now accept unused context parameter and renderColumn passes context through the accept chain for interface compliance.
E2E snapshot reflecting new DDL output
test/e2e/framework/test/ddl.test.ts
Inline snapshots updated to show PostgreSQL defaults now include explicit type casts: scheduled_at with ::timestamptz, JSONB defaults with ::jsonb.

🎯 3 (Moderate) | ⏱️ ~22 minutes

🐰 A context hops through the visitor's tale,
Type casts now flow like morning's dew,
PostgreSQL learns to set the scale—
When text is near, the cast bids adieu!
✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding explicit ::jsonb/::json casts to Postgres JSON column defaults, which aligns with the PR's primary objective and the substantial code changes across multiple files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tml-2861-pg-jsonb-default-cast

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@prisma-next/extension-author-tools

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-author-tools@763

@prisma-next/mongo-runtime

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-runtime@763

@prisma-next/family-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/family-mongo@763

@prisma-next/sql-runtime

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-runtime@763

@prisma-next/family-sql

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/family-sql@763

@prisma-next/extension-arktype-json

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-arktype-json@763

@prisma-next/middleware-cache

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/middleware-cache@763

@prisma-next/mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo@763

@prisma-next/extension-paradedb

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-paradedb@763

@prisma-next/extension-pgvector

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-pgvector@763

@prisma-next/extension-postgis

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-postgis@763

@prisma-next/postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/postgres@763

@prisma-next/sql-orm-client

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-orm-client@763

@prisma-next/sqlite

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sqlite@763

@prisma-next/extension-supabase

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-supabase@763

@prisma-next/target-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/target-mongo@763

@prisma-next/adapter-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/adapter-mongo@763

@prisma-next/driver-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/driver-mongo@763

@prisma-next/contract

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/contract@763

@prisma-next/utils

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/utils@763

@prisma-next/config

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/config@763

@prisma-next/errors

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/errors@763

@prisma-next/framework-components

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/framework-components@763

@prisma-next/operations

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/operations@763

@prisma-next/ts-render

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/ts-render@763

@prisma-next/contract-authoring

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/contract-authoring@763

@prisma-next/ids

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/ids@763

@prisma-next/psl-parser

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/psl-parser@763

@prisma-next/psl-printer

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/psl-printer@763

@prisma-next/cli

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/cli@763

@prisma-next/cli-telemetry

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/cli-telemetry@763

@prisma-next/emitter

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/emitter@763

@prisma-next/migration-tools

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/migration-tools@763

prisma-next

npm i https://pkg.pr.new/prisma/prisma-next@763

@prisma-next/vite-plugin-contract-emit

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/vite-plugin-contract-emit@763

@prisma-next/mongo-codec

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-codec@763

@prisma-next/mongo-contract

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-contract@763

@prisma-next/mongo-value

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-value@763

@prisma-next/mongo-contract-psl

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-contract-psl@763

@prisma-next/mongo-contract-ts

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-contract-ts@763

@prisma-next/mongo-emitter

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-emitter@763

@prisma-next/mongo-schema-ir

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-schema-ir@763

@prisma-next/mongo-query-ast

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-query-ast@763

@prisma-next/mongo-orm

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-orm@763

@prisma-next/mongo-query-builder

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-query-builder@763

@prisma-next/mongo-lowering

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-lowering@763

@prisma-next/mongo-wire

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-wire@763

@prisma-next/sql-contract-emitter

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-emitter@763

@prisma-next/sql-contract-psl

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-psl@763

@prisma-next/sql-contract-ts

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-ts@763

@prisma-next/sql-lane-query-builder

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-lane-query-builder@763

@prisma-next/sql-relational-core

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-relational-core@763

@prisma-next/sql-builder

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-builder@763

@prisma-next/sql-contract

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract@763

@prisma-next/sql-errors

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-errors@763

@prisma-next/sql-operations

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-operations@763

@prisma-next/sql-schema-ir

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-schema-ir@763

@prisma-next/target-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/target-postgres@763

@prisma-next/target-sqlite

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/target-sqlite@763

@prisma-next/adapter-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/adapter-postgres@763

@prisma-next/adapter-sqlite

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/adapter-sqlite@763

@prisma-next/driver-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/driver-postgres@763

@prisma-next/driver-sqlite

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/driver-sqlite@763

commit: 3f48bbe

@github-actions

github-actions Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

size-limit report 📦

Path Size
postgres / no-emit 147.91 KB (+0.05% 🔺)
postgres / emit 117.71 KB (+0.06% 🔺)
mongo / no-emit 76.67 KB (0%)
mongo / emit 70.96 KB (0%)
cf-worker / no-emit 177.86 KB (+0.06% 🔺)
cf-worker / emit 144.42 KB (+0.08% 🔺)

@wmadden-electric wmadden-electric changed the title TML-2861: restore ::jsonb / ::json cast on PG JSON column defaults TML-2861: emit ::jsonb / ::json cast on Postgres JSON column defaults Jun 8, 2026
Comment on lines +99 to +105
// JSON object/array literal → explicit `::<jsonb|json>` cast so the
// emitted DDL matches the column type without relying on Postgres's
// implicit text → jsonb coercion at default-evaluation time. Matches
// the pre-#751 planner-side `renderDefaultLiteral` behaviour for
// jsonb / json columns. For other columns whose underlying value is
// an object/array, fall back to a quoted literal — the column type
// decides.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No transient project references in code

@wmadden-electric wmadden-electric Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +106 to +110
const json = `'${escapeLiteral(JSON.stringify(value))}'`;
if (ctx.nativeType === 'jsonb' || ctx.nativeType === 'json') {
return `DEFAULT ${json}::${ctx.nativeType}`;
}
return `DEFAULT ${json}`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does JSON, specifically, need special handling? Won't this cast be required for any native type that isn't a string, boolean or simple number?

@wmadden-electric wmadden-electric Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two review findings on the earlier commit, both addressed here.

Generalize the cast rule (review at line 110). The earlier change
special-cased `jsonb` / `json`. But the same logic applies to ANY
non-text column type whose implicit text → target coercion is missing
or fragile — uuid, tstzrange, date, timestamptz, citext, enums,
user-defined types, geometry/geography, array types. The generalized
shape: any string-shaped literal default (a string, or a JSON object /
array serialised as JSON) emits an explicit `::<nativeType>` cast,
UNLESS the column type is text-shaped (text / varchar / character /
char and their parameterised forms), where the cast would be a no-op.
Numeric, boolean, and null literals are typed by Postgres directly
(no `text` indirection) so they need no cast.

Drop the transient project attribution (review at line 105). The
earlier comment said "Matches the pre-#751 planner-side
`renderDefaultLiteral` behaviour for jsonb / json columns" — that's
prose attribution to a prior change, exactly the pattern the
`no-transient-project-ids-in-code` rule names as forbidden ("post-M2
follow-up", "out of scope per the spec", "deferred per project plan").
Rewritten to describe what the code does, without referring to
historical state. Same applies to the test names — dropped the
`(TML-2861)` decorations; tests now name the behaviour they pin (per
the rule's "name the property the test pins" guidance).

Test coverage expanded to match the generalized rule:

- string literal on `uuid` / `tstzrange` / `date` columns gets an
  explicit `::<nativeType>` cast (the non-trivial cases the original
  jsonb/json fix missed).
- string literal on text-shaped columns (`text`, `varchar(N)`,
  `character varying(N)`, `char(N)`, `character(N)`) stays cast-free.
- numeric / boolean / null literals stay cast-free regardless of
  column type (including null on a non-text column).
- function defaults stay cast-free — a `DEFAULT (expr)` already
  returns the column type.

The existing jsonb-with-quoted-JSON test stays as before — `jsonb` is
non-text, so the generalized rule still emits `::jsonb`. The e2e
`ddl.test.ts` snapshot gains a `::timestamptz` cast on the
`scheduled_at` column (string literal on a non-text column); all
other snapshot lines are unchanged.

Verification (worktree-local):

- `@prisma-next/sql-relational-core` test: 331/331.
- `@prisma-next/target-postgres` test: 272/272.
- `@prisma-next/adapter-postgres` test: passes (intermittent
  Test/Hook-timed-out-in-100ms environmental flakes on parallel runs
  surfaced 3-6 failures across two runs — different tests each time,
  all timeouts, none related to this diff).
- `@prisma-next/target-sqlite` test: 111/111.
- `@prisma-next/adapter-sqlite` test: 178/178.
- `pnpm lint:deps`: clean.
- `pnpm fixtures:check`: clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden-electric

wmadden-electric commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Honest answer: the rule is alwaysApply: true but ships as guidance the author has to remember to check. No mechanical enforcement.

Concrete proposal: scripts/check-no-transient-refs.mjs wired into lint-staged.config.mjs, mirroring scripts/lint-deps-focused.mjs. Scans staged diffs for:

  • The ID-form regex from the rule's "Quick check before committing" section.
  • Prose-attribution patterns: pre-#NNN, post-#NNN, per the (spec|plan), sub-spec, out of scope per, deferred per, projects/<x>/.
  • Test-name decorations: it('… (TICKET-NNN)').

Allowlist: projects/**, wip/**, docs/architecture docs/** (per the rule).

Filing as a follow-up ticket unless you want it bundled — small script, easy either way.

wmadden and others added 2 commits June 8, 2026 11:03
…pt signature

`DdlColumnDefault.accept` now requires a `DdlColumnRenderContext` second
argument; the in-package visitor test was on the old one-arg signature
and broke `pnpm typecheck` on CI. Pass `{ nativeType: 'text' }` (any
text-shaped type works — the test asserts the visitor dispatches, not
the renderer output).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants