Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.61.1 introduces bug in typescript generation for computed column #568

Open
2 tasks done
ecchochan opened this issue Apr 11, 2023 · 3 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@ecchochan
Copy link

Bug report

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

Describe the bug

The below line introduced the bug in v0.61.1

pg_get_function_result(f.oid) as return_type,

- rt.typname as return_type,
+ pg_get_function_result(f.oid) as return_type,

The above results in computed column typehint always equal to unknown

To Reproduce

Setup Supabase with the following schema:

CREATE TABLE people (
  fname text,
  lname text
);

CREATE FUNCTION full_name(people) RETURNS text AS $$
  SELECT $1.fname || ' ' || $1.lname;
$$ LANGUAGE SQL;

Run

node \
  --no-warnings \
  dist/server/server.js \   
  gen types typescript \
  --include-schemas public

Or the following if docker-compose is setup with meta using v0.61.1:

docker-compose exec -T meta node \
  --no-warnings \
  dist/server/server.js \   
  gen types typescript \
  --include-schemas public

Expected behavior

The typescript generated should be

export interface Database {
  public: {
    Tables: {
      account: {
        Row: {
          fname: string | null;
          lname: string | null;
          full_name: string | null;
        };
      }
    }
...

Screenshots

The typescript generated is however

export interface Database {
  public: {
    Tables: {
      account: {
        Row: {
          fname: string | null;
          lname: string | null;
          full_name: unknown | null;
        };
      }
    }
...

System information

  • OS: macOS
  • Browser (if applies): N/A
  • Version of supabase-js: N/A
  • Version of Node.js: N/A

Additional context

Reverting the changes of the below resolve the issue

pg_get_function_result(f.oid) as return_type,

- rt.typname as return_type,
+ pg_get_function_result(f.oid) as return_type,
@ecchochan ecchochan added the bug Something isn't working label Apr 11, 2023
@leomeneguzzi
Copy link

leomeneguzzi commented Oct 13, 2023

In the actual version is even worse
const temp: PostgrestSingleResponse<SelectQueryError<"Referencing missing column 'full_name '">[]>

The full_name property don't appear inside the table type, just on the generated types as funcion

@leomeneguzzi
Copy link

Actually, I found the problem in my case... still a bug, I'm trying to do some workaround on my project.

The problem is here: https://github.com/supabase/postgres-meta/blob/HEAD/src/server/templates/typescript.ts#L101
.filter((fn) => fn.argument_types === table.name)

The parameter of my function is from another schema. Then the fn.argument_types property has the value like customschema.people, while the table.name is just people

@leomeneguzzi
Copy link

In case someone also faces this problem, the way that I solve it was:

import { Database, Json } from "./supabase.ts";

export interface CustomDatabase extends Database {
  gestaolocador: {
    Tables: {
      rentals: {
        Row: {
          last_due_date: string | null;
          next_due_date: string | null;
        } & Database["gestaolocador"]["Tables"]["rentals"]["Row"];
      } & Database["gestaolocador"]["Tables"]["rentals"];
    };
    Functions: {
        get_rental_by_user: {
          Args: {
            _user_id: string
          }
          Returns: {
              last_due_date: string | null;
              next_due_date: string | null;
          }[]
        }
    }
  } & Database["gestaolocador"];
}

export type { CustomDatabase as Database, Json };

I created a custom Database interface that complements the missing fields. This way I can still regenerate the types without redoing my manual changes every time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants