Skip to content

supabase-js: Selecting computed text field results in SelectQueryError #1034

@stefan-girlich

Description

@stefan-girlich

Describe the bug

Selecting a computed field on a table results in a TypeScript error:

// SelectQueryError<"column 'name_translated' does not exist on 'category'.">[] | null
const { data: data_computed_field } = await supabase
  .from("category")
  .select("name, name_translated");

Querying a computed relationship works as expected. See Steps to reproduce below and the linked MRE for further details.

Library affected

supabase-js

Reproduction

https://codesandbox.io/p/sandbox/89mt23

Steps to reproduce

  1. For a given table, create a Postgres function that accepts the table and returns a scalar, and thus can be used as a computed field in PostgREST; see example below
  2. Generate TS types, i.e. via CLI
  3. Initialize client via createClient<Database>(/* ... */)
  4. Query computed field => SelectQueryError

Computed fields vs computed relationships

Computed relationships work as expected; see the linked MRE. This is the case because they are added explicitly as functions and also in Database['public']['your_table_name']['Row'] whereas there is no entry for computed fields (^= functions returning a scalar).

Adding the missing computed field name during generation (or for now: manually) fixes the issue, but I cannot tell if this is the ideal solution or if anything speaks against it.

Special case

In my specific case, separate name_translated functions exists for two tables, resulting in a generated TS type like this:

name_translated:
        | {
            Args: { category: Database['public']['Tables']['category']['Row'] }
            Returns: string
          }
        | {
            Args: { something_else: Database['public']['Tables']['something_else']['Row'] }
            Returns: string
          }

Using different function/field names does not prevent the issue, but a fix may need to take the possibility of identical function/field names into consideration.

Postgres function example

CREATE OR REPLACE FUNCTION name_translated(category category)
RETURNS text
LANGUAGE sql
STABLE
AS $$
-- ... actual logic omitted ...
$$;

System Info

Reproducible on arbitrary system; tested on Codesandbox, Linux, MacOS

Supabase CLI v2.67.1
supabase-js v2.89.0
TypeScript v5.5.4

Used Package Manager

bun

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions