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

Wrong type for joins with one-to-one relationships #471

Open
2 tasks done
probablykasper opened this issue Aug 4, 2023 · 9 comments
Open
2 tasks done

Wrong type for joins with one-to-one relationships #471

probablykasper opened this issue Aug 4, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@probablykasper
Copy link

probablykasper commented Aug 4, 2023

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:

@probablykasper probablykasper added the bug Something isn't working label Aug 4, 2023
@aslakhellesoy
Copy link

aslakhellesoy commented Aug 7, 2023

I'm using this as a workaround for now:

export function cast<T>(notAnArray: T[]): T {
  return notAnArray as T;
}

And then:

console.log(cast(profile_result.data.customers).stripe_customer_id)

@aslakhellesoy
Copy link

Update: I've changed my workaround to this:

export function fixOneToOne<T>(objectOrNull: T[]): T | null {
  return (objectOrNull as T) || null;
}

I've changed the return type to T | null because the relationship may indeed be null. In the example above, this would happen when there is no customers record for the given profile.

In the example above, the correct type should be:

{
    stripe_customer_id: string;
} | null

@steve-chavez steve-chavez transferred this issue from supabase/supabase-js Aug 28, 2023
@FocusCookie
Copy link

I encountered the same issue today and I found another workaround.

const profile_result = await supabase
	.from('profiles')
	.select('id, customers ( stripe_customer_id )')
	.eq('id', params.id)
	.single()
        .returns<YourType[]>();

# type definition
type YourType {
    the type that you expect without the array
}

@timlgl
Copy link

timlgl commented Sep 12, 2023

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?

@orels1
Copy link

orels1 commented Dec 17, 2023

Seems to still be an issue

@albertonii
Copy link

Same problem over here, is there a fix for this issue??

@Silventino
Copy link

Same problem here.

@DhenPadilla
Copy link

Yep, same here! Please fix when possible

@chrissy0
Copy link

Still a problem, please fix!

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

9 participants