postgrest-js
postgrest-js copied to clipboard
Typescript generated types not working for relationships
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!
Hi, any idea on this? Thanks!
Can you try upgrading the CLI and the SDK? The generated types look outdated to me
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:
Also, promoter column appears configured like this in the dashboard:
Hi, sorry to reask, but any idea on this? Thanks!
Can you set up an example repo where we can replicate this? It's difficult to reproduce the problem otherwise.
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?
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.
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!
I don't think we'll support using enums that way. What you can do instead is to stringify it:
.from(`${CollectionNames.Group}`)
Okay! Then, resolved. Thanks!