Skip to content

Add note about typescript support for fetching #959

Open
@TimurKr

Description

@TimurKr

Improve documentation

In the documentation for fetching, under Query the same referenced table multiple times, it states an approach, which doesn't infer correct types on the resulting object.

Link

https://supabase.com/docs/reference/javascript/select

Describe the problem

The documentation states this approach

const { data, error } = await supabase
  .from('messages')
  .select(`
    content,
    from:sender_id(name),
    to:receiver_id(name)
  `)

This works and fetches the correct data, but with typescript, the types are incorrect; from and to are both typed as {}[].

Describe the improvement

I believe the documentation should mention another possible approach, which is also mentioned in the PostgREST documentation, which returns the correct data and infers the types correctly:

const { data, error } = await supabase
  .from('messages')
  .select(`
      content,
      from:users!messages_from_fkey(name),
      to:users!messages_from_fkey(name)
  `)

Here the users is the name of the foreign table from which we are querying and messages_from_fkey and messages_from_fkey are the names of the foreign key constraints.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions