postgres-meta icon indicating copy to clipboard operation
postgres-meta copied to clipboard

Generated DB types do not include function property in Table

Open jimmythigpen opened this issue 1 year ago • 1 comments

Bug report

  • [x] I confirm this is a bug with Supabase, not with my own application.
  • [x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

We are working with an order table and see unexpected behavior when generating types. This behavior may be due to the unfortunate use of a reserved word as a table name. We are already planning to update the table name in the future which resolves this issue, but wanted to at least surface the behavior.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Create two tables with an id column, one called hotel, another called order.
  2. Run the below SQL to create functions
CREATE OR REPLACE FUNCTION public.test_func1(hotel)
 RETURNS text
 LANGUAGE sql
AS $function$
  SELECT $1.id::text;
$function$;
CREATE OR REPLACE FUNCTION public.test_func2("order")
 RETURNS text
 LANGUAGE sql
AS $function$
  SELECT $1.id::text;
$function$;
  1. Generate DB types using the supabase CLI (we use supabase gen types typescript --local --schema public > src/generated/supabase.ts)

Expected behavior

We expect to see the created functions added as properties in the generated DB types for the order and profile tables, but this is missing from the order table.

Screenshots

image image

Code snipping below showing the generated types not including test_func2 on the order table.

export type Json =
  | string
  | number
  | boolean
  | null
  | { [key: string]: Json | undefined }
  | Json[]

export type Database = {
  public: {
    Tables: {
      hotel: {
        Row: {
          id: number
          test_func1: string | null
        }
        Insert: {
          id?: number
        }
        Update: {
          id?: number
        }
        Relationships: []
      }
      order: {
        Row: {
          id: number
        }
        Insert: {
          id?: number
        }
        Update: {
          id?: number
        }
        Relationships: []
      }
    }
    Views: {
      [_ in never]: never
    }
    Functions: {
      test_func1: {
        Args: {
          "": unknown
        }
        Returns: string
      }
      test_func2: {
        Args: {
          "": unknown
        }
        Returns: string
      }
    }
    Enums: {
      [_ in never]: never
    }
    CompositeTypes: {
      [_ in never]: never
    }
  }
}

type PublicSchema = Database[Extract<keyof Database, "public">]

export type Tables<
  PublicTableNameOrOptions extends
    | keyof (PublicSchema["Tables"] & PublicSchema["Views"])
    | { schema: keyof Database },
  TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
    ? keyof (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
        Database[PublicTableNameOrOptions["schema"]]["Views"])
    : never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
  ? (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
      Database[PublicTableNameOrOptions["schema"]]["Views"])[TableName] extends {
      Row: infer R
    }
    ? R
    : never
  : PublicTableNameOrOptions extends keyof (PublicSchema["Tables"] &
        PublicSchema["Views"])
    ? (PublicSchema["Tables"] &
        PublicSchema["Views"])[PublicTableNameOrOptions] extends {
        Row: infer R
      }
      ? R
      : never
    : never

export type TablesInsert<
  PublicTableNameOrOptions extends
    | keyof PublicSchema["Tables"]
    | { schema: keyof Database },
  TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
    ? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
    : never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
  ? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends {
      Insert: infer I
    }
    ? I
    : never
  : PublicTableNameOrOptions extends keyof PublicSchema["Tables"]
    ? PublicSchema["Tables"][PublicTableNameOrOptions] extends {
        Insert: infer I
      }
      ? I
      : never
    : never

export type TablesUpdate<
  PublicTableNameOrOptions extends
    | keyof PublicSchema["Tables"]
    | { schema: keyof Database },
  TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
    ? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
    : never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
  ? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends {
      Update: infer U
    }
    ? U
    : never
  : PublicTableNameOrOptions extends keyof PublicSchema["Tables"]
    ? PublicSchema["Tables"][PublicTableNameOrOptions] extends {
        Update: infer U
      }
      ? U
      : never
    : never

export type Enums<
  PublicEnumNameOrOptions extends
    | keyof PublicSchema["Enums"]
    | { schema: keyof Database },
  EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database }
    ? keyof Database[PublicEnumNameOrOptions["schema"]]["Enums"]
    : never = never,
> = PublicEnumNameOrOptions extends { schema: keyof Database }
  ? Database[PublicEnumNameOrOptions["schema"]]["Enums"][EnumName]
  : PublicEnumNameOrOptions extends keyof PublicSchema["Enums"]
    ? PublicSchema["Enums"][PublicEnumNameOrOptions]
    : never

System information

  • OS: [e.g. macOS]
  • Version of supabase-js: [e.g. 2.39.7]
  • Version of Node.js: [e.g. 20.11.0]

Additional context

Appreciate anyone taking the time to check this one out!

jimmythigpen avatar Jun 05 '24 22:06 jimmythigpen

I assume this is because the function argument "order" is quoted, and the quotes result in an inability to match with the table name. Some of the code in this repo for pulling the schema is using pg_get_function_arguments() which does return "order" for this function.

pangolingo avatar Jun 05 '24 23:06 pangolingo

I want to work on this

Suhas-30 avatar Aug 11 '25 12:08 Suhas-30