postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

Typescript generated types not working for relationships

Open dreinon opened this issue 8 months ago • 5 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

I have set up generated typescript types in my project and they work well until I try to get related data.

I have a call like this: supabase.from('group').select('displayName, event (id)')

And it outputs the following return type: { error: true; } & "could not find the relation between group and event"

If you ignore typescript errors, the query works without errors and gets the correct embeded data.

I attach the chunk of the generated db types from both event and group tables. This is a one-to-many relationship, one group can have many events assigned (through promoter column in event table). Promoter is not primary key but since it is not a many-to-many relationship I think it is not necessary, right? I would appreciate some help. Thanks!

Image

dreinon avatar Mar 11 '25 10:03 dreinon

Hi, any idea on this? Thanks!

dreinon avatar Mar 25 '25 08:03 dreinon

Can you try upgrading the CLI and the SDK? The generated types look outdated to me

soedirgo avatar Mar 26 '25 02:03 soedirgo

Hi @soedirgo, I updated everything but the exact same type error is still there. I remind that the call to supabase actually returns the correct data, it's only the types that fail.

Versions:

cli: "supabase": "^2.20.12",
ssr: "@supabase/ssr": "^0.6.1"
supabase-js: "@supabase/supabase-js": "^2.49.4"

My types look like this now: Image

Also, promoter column appears configured like this in the dashboard:

Image

dreinon avatar Apr 10 '25 17:04 dreinon

Hi, sorry to reask, but any idea on this? Thanks!

dreinon avatar May 05 '25 14:05 dreinon

Can you set up an example repo where we can replicate this? It's difficult to reproduce the problem otherwise.

soedirgo avatar May 06 '25 17:05 soedirgo

In the example repo, it does work, but after hours of trying to debug what is happening, i cannot find it. The version of supabase-js and postgrest-js is the same in both repos, the typescript version is the same and the tsconfig also. It should be another dependency or something, but it's driving me nuts. Using vs-code in both. Any advice?

dreinon avatar Jun 24 '25 18:06 dreinon

How are you invoking the Supabase CLI? Can you try invoking it with npx supabase@latest?

Also, can you check if the contents of ./supabase/.temp/postgres-version is the same in both repos?

Other than that, there's not much we can help without access to your project or an example repo.

soedirgo avatar Jun 25 '25 07:06 soedirgo

Hi, I've found the problem. As simple as using an Enum item instead of the regular string:

enum CollectionNames {
	Group = 'group',
}

type DbSupabaseClient = SupabaseClient<Database>

const query = (
	supabase: DbSupabaseClient
) => {
	return supabase
		.from(CollectionNames.Group)
		.select('id, event (id, promoter)')
		.single();
};

type QueryReturnType = ReturnType<typeof query>

This returns the type:

PostgrestBuilder<{
    id: string;
    event: SelectQueryError<"could not find the relation between group and event">;
}, false>

as specified in the issue description.

@soedirgo Do you think this is an issue? Should I close this issue and open one for enum compatibility? Thanks!

dreinon avatar Jul 01 '25 13:07 dreinon

I don't think we'll support using enums that way. What you can do instead is to stringify it:

		.from(`${CollectionNames.Group}`)

soedirgo avatar Jul 03 '25 18:07 soedirgo

Okay! Then, resolved. Thanks!

dreinon avatar Jul 07 '25 11:07 dreinon