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

Type error when ordering by a foreign column

Open jojomatik opened this issue 3 years ago • 2 comments

Bug report

Describe the bug

A type error is shown if trying to order by a foreign table.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

let baseQuery = getSupabaseClient()
      .from<AuthorTypeWithBooks>("author")
      .select("*, books:book!authorId!inner(*)")
      .order("title", { foreignTable: "book" });

=> TS2345: Argument of type '"title"' is not assignable to parameter of type 'keyof AuthorType | "books"'.

Relevant type definitions:

export type AuthorTypeWithBooks = AuthorType & {
  books?: BookType[];
};

export type AuthorType = {
  id?: string;
  name?: string;
};

export type BookType = {
  id?: string;
  authorId?: string;
  title?: string;
};

Expected behavior

No type error occurs.

Screenshots

Not applicable.

System information

  • OS: Windows
  • Browser (if applies): chrome
  • Version of supabase-js: 1.35.6
  • Version of Node.js: v16.13.0

Additional context

It is possible to get around this with a workaround injecting title into the type:

let baseQuery = getSupabaseClient()
      .from<AuthorTypeWithBooks & { title: string }>("author")
      .select("*, books:book!authorId!inner(*)")
      .order("title", { foreignTable: "book" });

jojomatik avatar Sep 03 '22 19:09 jojomatik

Can you do it with @supabase/supabase-js@rc? We'll be releasing it as @2 in a few weeks. @supabase/supabase-js@1 likely won't be updated anymore.

soedirgo avatar Sep 05 '22 08:09 soedirgo

Thanks for the quick reply. I haven't found the time to test yet and am going on vacation, I'll try to take a look afterwards

jojomatik avatar Sep 09 '22 21:09 jojomatik