Repository navigation
fix(postgrest-typegen): generated columns, NonNullable json, zero-arg Args, trigger-writable views - #125
Merged
Conversation
… Update types `GENERATED ALWAYS AS ... STORED` columns are not writable through PostgREST, but the TypeScript generator only excluded identity ALWAYS columns. Insert and Update now emit `?: never` for generated columns as well, while the column stays on Row. Ported from supabase/postgres-meta#1105.
…le<Json> The generated Json type itself includes null, so a NOT NULL json/jsonb column previously typed as plain Json still structurally permitted null. Narrowing with NonNullable in the single nullability chokepoint covers Row, Insert and Update consistently; nullable json columns keep the plain Json union since it already covers null. Ported from supabase/postgres-meta#1085.
…opertyKey, never>
`Args: never` breaks postgrest-js's computed-field detection
(`never extends { '': Row }` is always true), so a zero-argument
function sharing its name with a table column caused that column to be
omitted from select('*') result types. An object type with a required
never property is also uninhabited, collapsing the whole Database type
for tools doing sound type math on it (e.g. JSON Schema converters).
Record<PropertyKey, never> is the accurate type for a function callable
with no arguments, restores CLI v1.x behavior, and is what postgrest-js
already special-cases in IsMatchingArgs.
The parity golden changes are the fourteen zero-argument functions in
the fixture whose Args switch from never to Record<PropertyKey, never>
(some entries reflow to multiple lines under the formatter's print
width); nothing else in the golden changes.
Ported from supabase/postgres-meta#1035 (the zero-argument part; its
computed-field filtering change is already superseded by the current
generator, which introspects table/view row types via
includeTableTypes).
… OF trigger views Insert and Update emission for views was gated solely on is_updatable (pg_relation_is_updatable without triggers), so complex views made writable by INSTEAD OF triggers got no Insert or Update types even though PostgREST can write through them. Views now carry is_insert_enabled and is_update_enabled, computed with pg_relation_is_updatable(oid, true) so INSTEAD OF triggers (and INSTEAD rules) count, and the generator gates the Insert and Update blocks on them independently: a view with only an INSTEAD OF INSERT trigger gets only an Insert type. Column updatability likewise counts INSTEAD OF UPDATE triggers via pg_column_is_updatable(oid, attnum, true) plus an explicit INSTEAD OF INSERT trigger check (pg_column_is_updatable only considers the UPDATE event), so trigger-writable view columns no longer degrade to `?: never`. The new PostgresView fields are an additive extension over postgres-meta's original contract, mirrored in the frozen equivalence interfaces. Materialized views keep both flags false. The parity fixture has no trigger-backed views, so goldens are unchanged. Ported from supabase/postgres-meta#1062, reimplemented: the original PR hand-rolled duplicated pg_trigger bitmask subqueries (one pair with wrong bit values) and left trigger-backed view columns non-updatable, which degraded its own Insert/Update output to all-never fields.
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 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. Comment |
This was referenced Aug 31, 2026
Closed
fix(typescript): generate non-nullable json columns as NonNullable<Json>
supabase/postgres-meta#1085
Closed
Closed
mandarini
approved these changes
Aug 31, 2026
Closed
This was referenced Aug 31, 2026
Closed
spydon
pushed a commit
that referenced
this pull request
Sep 2, 2026
🤖 I have created a release *beep* *boop* --- ## [0.2.1](postgrest-typegen-v0.2.0...postgrest-typegen-v0.2.1) (2026-09-02) ### Bug Fixes * **postgrest-typegen:** escape database values in Swift string literals ([#123](#123)) ([4a5cef5](4a5cef5)) * **postgrest-typegen:** escape Go struct tags for pathological column names ([#122](#122)) ([42c0249](42c0249)) * **postgrest-typegen:** generated columns, NonNullable json, zero-arg Args, trigger-writable views ([#125](#125)) ([67650c6](67650c6)) * **postgrest-typegen:** inline pg-format literal and drop the dependency ([#138](#138)) ([2c16a15](2c16a15)) * **postgrest-typegen:** python escaping, 3.9 imports, JsonValue json columns, composite nullability ([#124](#124)) ([d1f9b3b](d1f9b3b)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: supabase-releaser[bot] <223506987+supabase-releaser[bot]@users.noreply.github.com>
spydon
added a commit
that referenced
this pull request
Sep 2, 2026
…ated columns, legacy metadata compatibility (#128) ## Summary Adversarial re-review of #125 (merged) surfaced three defects in its trigger-writable views and generated-columns work; this PR carries the fixes, each verified against real Postgres containers. 1. **Update-only trigger views emitted all-`never` Update types.** `pg_column_is_updatable(oid, attnum, TRUE)` requires the relation to support both UPDATE and DELETE, so it is false for every trigger-only view (verified on PostgreSQL 15), and #125's trigger check matched only INSTEAD OF INSERT triggers. Column updatability now checks INSTEAD OF triggers for INSERT or UPDATE (`tgtype & 64` and `& 20`) and unconditional INSTEAD rules (`pg_rewrite` with `ev_type` 2 or 3 and empty qualification), with the auto-updatable path back on `pg_column_is_updatable(oid, attnum, FALSE)`. The integration test now covers the full matrix: insert-only trigger, update-only trigger, both triggers, unconditional insert rule, conditional rule, and a read-only join view. 2. **PostgreSQL 18 virtual generated columns were emitted as writable.** `attgenerated` matching now includes `'v'` alongside `'s'` (VIRTUAL is the default for `GENERATED ALWAYS AS` on 18, and neither variant accepts writes, proven against a postgres:18 container). New integration test covers both variants end to end. 3. **The new view flags broke the version 1 metadata contract.** `is_insert_enabled`/`is_update_enabled` were required on `postgresViewSchema`, so `parseGeneratorMetadata` rejected previously valid documents produced by released code. The fields are now optional and the generator falls back to `is_updatable` when they are absent, with regression tests for legacy documents. ## Validation - `check-types`, `format-and-lint`, `knip`, `build`, full test suite (102 pass, Docker-backed introspection matrix included) all green on top of current main. - Parity goldens unchanged.
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.
Summary
Ports the worthwhile TypeScript generator fixes from postgres-meta's open template PRs into this package (the templates are being deleted in favor of this package in supabase/postgres-meta#1084, so open fixes there are triaged and re-landed here). Four fixes, one commit each:
GENERATED ALWAYS AS ... STOREDcolumns reject writes in Postgres, but only identity-ALWAYS columns were excluded; both now emit?: never.NonNullable<Json>(from fix(typescript): generate non-nullable json columns as NonNullable<Json> postgres-meta#1085): the emittedJsontype includesnull, so a NOT NULL json/jsonb column structurally permitted null. Known accepted edge: a NOT NULL jsonb column holding a JSON'null'::jsonbvalue still serializes as JSnull, so Row is optimistic in that case; Insert/Update narrowing is fully sound.Record<PropertyKey, never>(the still-valid half of fix: function type generation for zero arg and scalar computed fields postgres-meta#1035):Args: nevermakes postgrest-js treat every zero-argument function as a computed field (never extends { '': Row }always holds), dropping same-named columns fromselect('*')results, and an uninhabitedDatabasebreaks sound type tooling. Verified against postgrest-js, whoseIsMatchingArgsspecial-casesRecord<PropertyKey, never>.is_insert_enabled/is_update_enabledcomputed viapg_relation_is_updatable(oid, true)(bit 8 INSERT, bit 4 UPDATE; also covers INSTEAD rules), gated independently, and column updatability counts triggers too (pg_column_is_updatable(oid, attnum, true)plus an explicit INSTEAD OF INSERT trigger check, since that function only considers the UPDATE event). The origin PR duplicated hand-rolled pg_trigger subqueries with one pair of wrong bit values and left trigger-writable columns degrading to?: never, visible in its own snapshot. The two newPostgresViewfields are additive (metadata version stays 1), documented, and mirrored in the frozen equivalence contract.Triage of origin PRs
is_generatedwas already introspected.includeTableTypes: true, so table/view row types already resolve (parity golden shows computed fields working). Only foreign-table row types remain uncovered; the PR's name-string matching is too fragile to port for that niche.neverUpdate output in its own snapshot).number[])Jsontounknown)bigint)bigint_asoption)Validation
Args: nevertoArgs: Record<PropertyKey, never>. Fixes 1, 2 and 4 have no fixture-visible effect.check-types,format-and-lint,knip,build,test(99 pass across 12 files) all green.