supabase / postgres-meta

A RESTful API for managing your Postgres. Fetch tables, add roles, and run queries
https://supabase.com
Apache License 2.0
941 stars 126 forks source link

supabase gen types generates duplicate identifiers #590

Closed monodop closed 1 year ago

monodop commented 1 year ago

Bug report

Describe the bug

running supabase gen types typescript generates an invalid typescript typing:

To Reproduce

create table
  public.users (
    id uuid not null,
    name character varying(255) not null,
    role public.role not null default 'unapproved'::role,
    constraint users_pkey primary key (id),
    constraint id foreign key (id) references auth.users (id),
    constraint name_length check ((length((name)::text) > 3)),
    constraint name_valid_whitespace check (((name)::text !~ '(^\s)|(\s$)|(\s\s)'::text))
  ) tablespace pg_default;

create type
  public.role as enum ('admin', 'approved', 'unapproved');
npx supabase gen types typescript --local --schema public > $PROJECT_PATH/types/supabase_gen.ts
export type Json =
  | string
  | number
  | boolean
  | null
  | { [key: string]: Json }
  | Json[]

export interface Database {
  public: {
    Tables: {
      users: {
        Row: {
          id: string
          name: string
          name: string
          role: Database["public"]["Enums"]["role"]
        }
        Insert: {
          id: string
          name: string
          name: string
          role?: Database["public"]["Enums"]["role"]
        }
        Update: {
          id?: string
          name?: string
          name?: string
          role?: Database["public"]["Enums"]["role"]
        }
        Relationships: [
          {
            foreignKeyName: "id"
            columns: ["id"]
            referencedRelation: "users"
            referencedColumns: ["id"]
          }
        ]
      }
    }
    Views: {
      [_ in never]: never
    }
    Functions: {
      [_ in never]: never
    }
    Enums: {
      role: "admin" | "approved" | "unapproved"
    }
    CompositeTypes: {
      [_ in never]: never
    }
  }
}

Expected behavior

the name field should not be duplicated in the output ts

image

System information

monodop commented 1 year ago

I believe it has to do specifically with this constraint. If I remove it, the issue goes away.

constraint name_valid_whitespace check (((name)::text !~ '(^\s)|(\s$)|(\s\s)'::text))
monodop commented 1 year ago

It looks like this same issue is also happening in the local table editor image

gregnr commented 1 year ago

Thanks for reporting @monodop! Going to transfer this to supabase/postgres-meta for further discussion.