postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

Exclude generated columns from the insert type

Open yannxaver opened this issue 2 years ago • 5 comments

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.

yannxaver avatar Feb 05 '23 10:02 yannxaver

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.

juanvilladev avatar Feb 17 '23 00:02 juanvilladev

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?

soedirgo avatar Feb 21 '23 08:02 soedirgo

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.

soedirgo avatar Feb 27 '23 08:02 soedirgo

If I run

alter table foo
add column bar numeric GENERATED ALWAYS AS (col_a + col_b) STORED

the generated type becomes

Insert: {
  bar?: number | null
}

// same for update

yannxaver avatar Mar 05 '23 12:03 yannxaver

Ah, thanks - I was able to reproduce this.

soedirgo avatar Mar 06 '23 10:03 soedirgo