supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.

Home Page:https://supabase.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exclude generated columns from the insert type

yannxaver opened this issue · comments

commented

Bug report

Describe the bug

Postgres allows the creation of generated columns: https://www.postgresql.org/docs/current/ddl-generated-columns.html. Inserting a value into such a column throws an error: https://www.tutorialsteacher.com/postgresql/generated-always. Supabase includes these columns in its table['Insert'] type which goes against the functionality of postgres.

To Reproduce

  • create generated column
  • generate types

Expected behavior

Exclude generated columns.

Yeah, this would be amazing. I don't see any use case for having the generated columns there. They shouldn't even be queryable by the Supabase Client IMO.

Hmm, I thought we already do exclude it: https://github.com/supabase/postgres-meta/blob/df5df091c2b33eaea2e747bdb5bce3d18f74dad6/src/server/templates/typescript.ts#L103

Do you have an example schema I could reproduce this with?

Tried reproducing this - here's what I got:

create table t (id int8 generated always as identity);
export type Json =
  | string
  | number
  | boolean
  | null
  | { [key: string]: Json }
  | Json[]

export interface Database {
  public: {
    Tables: {
      t: {
        Row: {
          id: number
        }
        Insert: {
          id?: never
        }
        Update: {
          id?: never
        }
      }
    }
    Views: {
      [_ in never]: never
    }
    Functions: {
      [_ in never]: never
    }
    Enums: {
      [_ in never]: never
    }
    CompositeTypes: {
      [_ in never]: never
    }
  }
}

The type is replaced with undefined, which is acceptable because undefined values are stripped out on JSON.stringify(). But you'll see an error if you supply a number there.