postgrest-js: Wrong type for joins with one-to-one relationships
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
I have a profiles and a customers table, with a one-to-one relationship:
profiles: {
id: text, primary key
...
}
customers: {
id: references profiles.id, primary key
stripe_customer_id: text, unique
...
}
When doing a join between these, TypeScript thinks the type of customers is this:
{
stripe_customer_id: string;
}[]
But when I run the code, this is the actual type I get:
{
stripe_customer_id: string;
}
So when accessing the stripe_customer_id property, I get this type error:
Property ‘stripe_customer_id’ does not exist on type ‘{ stripe_customer_id: string; }[]’.
To Reproduce
const profile_result = await supabase
.from('profiles')
.select('id, customers ( stripe_customer_id )')
.eq('id', params.id)
.single()
if (profile_result.data) {
console.log(profile_result.data.customers.stripe_customer_id)
// ^
// Property 'stripe_customer_id' does not exist on
// type '{ stripe_customer_id: string; }[]'.ts(2339)
}
Expected behavior
The TypeScript type should not be an array for one-to-one relationships. It should match the actual type
System information
- OS: macOS
- Version of supabase-js: 2.31.0
- Version of Node.js: 18.14.2
Additional context
Related:
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 25
- Comments: 21 (2 by maintainers)
Seems to still be an issue
I encountered the same issue today and I found another workaround.
Update: I’ve changed my workaround to this:
I’ve changed the return type to
T | nullbecause the relationship may indeed benull. In the example above, this would happen when there is nocustomersrecord for the given profile.In the example above, the correct type should be:
I am experiencing the exact same issue. As my query is rather nested, workarounds are very inconvenient…
Does anyone know, whether an older version does not have this bug included?
I’m using this as a workaround for now:
And then:
Yep, same here! Please fix when possible
Same problem over here, is there a fix for this issue??
I checked the schema in my environment, and the type relationships generated by Supabase with the same schema you provided were correctly inferred.
Based on your screenshot, I noticed that not only the one-to-one relationships, but also the text type columns are inferred as any type, which is concerning. Are the types being generated correctly by the Supabase CLI, and is the output file properly generated?
Here’s what I did
npx create-next-app -e with-supabaseAbsolutely not fixed. A one-to-one relationship should not expect an array in this case.
Tables
Typescript
Last version used (2.45.4)
Same problem here.
Hi there, would you mind providing a minimal reproduction example of your case with both the SQL to generate the relation and the select query you’re running ?
.singleshould only affect the “root result” not the underlying relationship type. If I can reproduce I could investigate into this, please feel free to open a new issue and ping me on it.I’m locking this issue as the original problem is fixed and similar ones will likely be under a different scope. Please open a new issue following the typing bug reporting infos:
Please fix!
same problem for me