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

NO Typing/ autocompletion hint working When filtering on Foreign keys - V2.

Open roker15 opened this issue 3 years ago • 3 comments

Here is Example link from supabase docs.

const { data, error } = await supabase
  .from('cities')
  .select('name, countries(*)')
  .eq('countries.name', 'Estonia')

In above example countries.name shows error in types. And no autocompletion available after countries.

roker15 avatar Oct 14 '22 08:10 roker15

I also have this issue. For example:

const result = await supabaseClient
    .from('Orders')
    .select(`
        order_id,
        Customers (
            name,
            customer_id
        )`)
    .eq('Customers.customer_id, 10);

result has the return type

PostgrestResponse<{
    order_status: number;
    id: number;
    created_at: string | null;
    customer_id: number;
} & {
    Customers: unknown;
}>

when I would expect the return type to be:

PostgrestResponse<{
    order_status: number;
    id: number;
    created_at: string | null;
    customer_id: number;
} & {
    Customers: {
        name: string,
        customer_id: number
    }
}>

Where the type of Customers properties should be determined from the Database schema.

This bug means that result.Customers.name is not possible due to "Customers" being unknown type, instead it must be done something like:

type Customer = Database['public']['Tables']['Customers']['Row'];
const customerName = (result.Customers as Customer).name;

Which is not ideal, as this approach will allow properties from the db missed from the query (e.g. if the table has a "email" field in customers table, but we forgot to include it in the query, (result.Customers as Customer).email would compile with TypeScript but throw an error at runtime).

matt-winfield avatar Oct 14 '22 16:10 matt-winfield

I can second this.

Even forcing on the from the type it does not work as <"table", Type> it works on the first one but always fails on relations.

pcardosolei avatar Oct 15 '22 00:10 pcardosolei