fix(typescript): exclude generated columns from Insert and Update types#1069
Open
selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
Conversation
PostgreSQL `GENERATED ALWAYS AS (expr) STORED` columns cannot be inserted or updated, but were incorrectly included in the TypeScript `Insert` and `Update` types. This fix adds a check for `column.is_generated` alongside the existing `identity_generation === 'ALWAYS'` check to exclude these columns from write types. Fixes supabase#838
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 #838
Problem
PostgreSQL
GENERATED ALWAYS AS (expr) STOREDcolumns cannot be inserted or updated — the database computes them automatically. However, they were incorrectly included in the TypeScriptInsertandUpdatetypes, which only excludedGENERATED ALWAYSidentity columns.Solution
Add a check for
column.is_generatedalongside the existingidentity_generation === 'ALWAYS'check in both theInsertandUpdatetype blocks. Whencolumn.is_generatedistrue, the column is rendered ascolumnName?: never(same as identity columns), preventing it from appearing as a writable field.Changes
src/server/templates/typescript.ts: Add|| column.is_generatedto the exclusion condition in bothInsertandUpdatetype generation blockstest/server/typegen.ts: Add a test case that creates a table with aGENERATED ALWAYS AS (...) STOREDcolumn and asserts it appears inRowbut is excluded (?: never) fromInsertandUpdate