supabase / cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
https://supabase.com/docs/reference/cli/about
MIT License
1.07k stars 209 forks source link

Issue with supabase gen types not correctly generating types for arrays of composite types #1329

Closed Latent-Labs-Inc closed 8 months ago

Latent-Labs-Inc commented 1 year ago

I'm using the supabase gen types command to generate types for my Supabase database. However, I've noticed that the command is not correctly generating types for arrays of composite types. Instead of the expected type, it's generating unknown for these types.

Here's the command I'm using:

npx supabase gen types typescript --project-id "MY_PROJECT_ID" > types/supabase.ts --debug

And here's how the types are being generated:

medicalinsuranceplan: {
  payer_id: string
  subscriber: Database["public"]["CompositeTypes"]["responsemember"]
  dependents: unknown
  payer: Database["public"]["CompositeTypes"]["payer"]
  plan_information: Database["public"]["CompositeTypes"]["planinformation"]
  plan_date_information: Database["public"]["CompositeTypes"]["plandateinformation"]
  benefits_information: unknown
  status: string
}

The dependents is just the same responsemember type but an array.

I have provided the plpgsql code as well below

create type medicalinsuranceplan as ( payer_id text, subscriber responsemember, dependents responsemember[], payer payer, plan_information planinformation, plan_date_information plandateinformation, benefits_information benefitsinformation[], status text );

As you can see, the dependents and benefits_information fields, which are arrays of composite types, are being generated as unknown.

I've tried updating to the latest version of the Supabase CLI, but the issue persists. I would appreciate any help or guidance on how to resolve this issue.

Thank you,

adam-kov commented 1 year ago

I have the same issue on the latest version (1.82.2) as of writing. I also tried creating a domain out of the type array like below, but it resulted in no difference.

CREATE TYPE inner_type ( ... );
CREATE DOMAIN inner_type_array AS inner_type[];
CREATE TYPE outer_type (
  property inner_type_array,
  ...
);

Temporary workaround:

As the inner composit type is generated correctly, running the following command after generating the types will insert the correct type.

sed -i s/'<PROPERTY_NAME>: unknown'/'<PROPERTY_NAME>: Database["public"]["CompositeTypes"]["<TYPE_NAME>"][]'/ ./<DESTINATION_FILE>

My full command to generate and replace types looks like this:

npx supabase gen types typescript \
--project-id "<PROJECT_ID>" \
--schema public > <DESTINATION_FILE> \
&& sed -i \
s/'<PROPERTY_NAME>: unknown'/'<PROPERTY_NAME>: Database["public"]["CompositeTypes"]["<TYPE_NAME>"][]'/ \
<DESTINATION_FILE>

_You need to replace <PROJECT_ID>, <DESTINATION_FILE>, <PROPERTY_NAME> and <TYPE_NAME>_

mlukasik-dev commented 1 year ago

I faced the same issue in my project, and I'm wondering if there are any plans to address it. Could you please provide an update on the status or any potential timelines for a fix? I'm eager to see this resolved. Thank you!