Skip to content

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

Closed
supabase/sdk
#137

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

Activity

  1. added
    bug
    on Jan 7, 2026
  2. added theissue type on Jan 7, 2026
  3. mandarini commented on Jan 7, 2026

    @mandarini
    Contributor

    @stefan-girlich thank you for filing this issue. I think this is an issue with how the CLI generates the types. The Row type represents "what fields can be selected from this table", so computed fields are selectable, so they belong in Row. The CLI is the source of truth for generated types as it reads the database schema and should include all selectable fields. postgrest-js already expects computed fields to be in Row (our test fixtures include blurb_message in messages.Row). Since computed relationships are already handled by the CLI, scalar computed fields should follow the same pattern. I think that the CLI should detect functions with signature function_name(table_name) RETURNS <scalar_type> and add them to the corresponding table's Row type. In any case, I will bring this to the attention of the team!

  4. 7ttp commented on Jan 24, 2026

    @7ttp
    Member

    @mandarini Umm, the CLI invokes type generation,
    but the actual schema detection and type generation logic lives in supabase/postgres-meta
    (see internal/gen/types/types.go it runs the pg-meta container).
    The fix for detecting scalar computed fields needs to be implemented in postgres-meta's type generation code,
    not the CLI.
    Could you pass this issue there? or should i create a new one 😅💚

  5. added theissue type on Jan 26, 2026
  6. transferred this issue fromsupabase/clion Jan 26, 2026
  7. icopp commented on Jan 27, 2026

    @icopp

    @stefan-girlich thank you for filing this issue. I think this is an issue with how the CLI generates the types. The Row type represents "what fields can be selected from this table", so computed fields are selectable, so they belong in Row. The CLI is the source of truth for generated types as it reads the database schema and should include all selectable fields. postgrest-js already expects computed fields to be in Row (our test fixtures include blurb_message in messages.Row). Since computed relationships are already handled by the CLI, scalar computed fields should follow the same pattern. I think that the CLI should detect functions with signature function_name(table_name) RETURNS <scalar_type> and add them to the corresponding table's Row type. In any case, I will bring this to the attention of the team!

    The problem with just adding them to Row is that if you select *, those functions aren't included in PostgREST results. The typing needs to be set up in a way that the function is only included in the output type if referenced by name.

  8. dan-hale commented on Feb 3, 2026

    @dan-hale

    @stefan-girlich thank you for filing this issue. I think this is an issue with how the CLI generates the types. The Row type represents "what fields can be selected from this table", so computed fields are selectable, so they belong in Row. The CLI is the source of truth for generated types as it reads the database schema and should include all selectable fields. postgrest-js already expects computed fields to be in Row (our test fixtures include blurb_message in messages.Row). Since computed relationships are already handled by the CLI, scalar computed fields should follow the same pattern. I think that the CLI should detect functions with signature function_name(table_name) RETURNS <scalar_type> and add them to the corresponding table's Row type. In any case, I will bring this to the attention of the team!

    The problem with just adding them to Row is that if you select *, those functions aren't included in PostgREST results. The typing needs to be set up in a way that the function is only included in the output type if referenced by name.

    This already exists for computed relationships, I'm sure that could be used as a reference for implementing this.

  9. mujeebmasi commented on Sep 2, 2026

    @mujeebmasi

    I looked into this and found the root cause — it explains why it reproduces for some schemas and not others.

    Type generation matches computed fields by comparing the function's argument_types against the table name:

    .filter(({ fn }) => fn.argument_types === table.name)
    

    argument_types is pg_get_function_arguments(), which renders the parameter name alongside its type whenever the parameter has one, and schema-qualifies the type when it isn't visible in search_path.

    So the function in this report:

    CREATE FUNCTION name_translated(category category) RETURNS text
    

    produces "category category", which never equals "category" — the computed field is dropped from Row. The unnamed form name_translated(category) produces "category", matches, and works. That's the whole difference between the schemas where this reproduces and the ones where it doesn't.

    @7ttp is right that the fix belongs in the generator rather than the CLI — though it has since moved again: the engine was extracted out of postgres-meta into @supabase/postgrest-typegen (in supabase/sdk), and postgres-meta/src/lib/generators.ts is now a thin adapter over its introspect().

    I've opened a PR there matching on the argument's composite type (via type_relation_id) instead of the rendered string, so both spellings resolve: supabase/sdk#137

    Happy to adjust the scope if maintainers prefer it narrowed.

  10. added a commit that references this issue on Sep 2, 2026
    a161fe2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions