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

Referencing missing column

Open ohrrkan opened this issue 1 year ago • 13 comments

@supabase/supabase-js": "^2.39.0" using Supabase CLI to [generate the types] "supabase": "^1.123.0"

Query the same referenced table multiple times work but give you a Typescript error as it expect an array.

to reproduce you can use the datasource (doc example)

create table
 users (id int8 primary key, name text);

 create table
   messages (
     sender_id int8 not null references users,
     receiver_id int8 not null references users,
     content text
   );

 insert into
   users (id, name)
 values
   (1, 'Kiran'),
   (2, 'Evan');

 insert into
   messages (sender_id, receiver_id, content)
 values
   (1, 2, '👋');

query :

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

now using data[0].sender_id.name will work but will give you an typescript error. "sender_id: SelectQueryError<"Referencing missing column name">[];

Bypass temporary solution :

type patch = { content: text; sender_id: Tables<"users">; receiver_id: Tables<"users">}[];

const { data, error } = await supabase
  .from('messages')
  .select(`
    content,
    sender_id(name),
    receiver_id(name)
  `).returns<patch>()

ohrrkan avatar Dec 20 '23 10:12 ohrrkan

Is there any PR which fixes those? I recently updated supabase-js version and generated new types and got similar issue, some database fields are not recognized anymore. I cant pinpoint the exact moment it stated to break tho.

Edit: For me this solved it - https://github.com/supabase/supabase-js/issues/974

dvsmc avatar Feb 13 '24 18:02 dvsmc

Same problem for me even when switching to 2.39.3 image

nabilhayek avatar May 18 '24 05:05 nabilhayek

i got the same error

RiodeJaneiroo avatar Jun 01 '24 14:06 RiodeJaneiroo

Same here!

AndrianMauricio avatar Jun 03 '24 00:06 AndrianMauricio

same here...

yaberkane05 avatar Jul 02 '24 23:07 yaberkane05

still an issue

rnnc avatar Jul 18 '24 17:07 rnnc

Same here. Weirdly, this works...

supabase
  .from('selection_assets')
  .select(
    `
    id,
    comment,
    order,
    created_at,
    asset: assets (
      id,
      name,
      is_listed
    )
  `,
  )
  .eq('selection_id', selectionId)
  .single();

and this doesn't...

supabase
  .from('selections')
  .select(
    `
    *,
      client: client_id(id)
    `,
  )
  .eq('id', selectionId)
  .single();

artilishes avatar Aug 08 '24 14:08 artilishes

still an issue

theshadowagent avatar Aug 15 '24 17:08 theshadowagent

Got the issue too

Th1nhNg0 avatar Aug 16 '24 12:08 Th1nhNg0

Same issue

MattieTK avatar Aug 19 '24 23:08 MattieTK

Any solution?

silvaMatheus avatar Aug 27 '24 03:08 silvaMatheus

I have the feeling it depends on in which direction or on which side the relation is defined. Still need to verify this.

karladler avatar Aug 28 '24 22:08 karladler

@ohrrkan As mentioned in the documentation here: https://supabase.com/docs/reference/javascript/select (example for 'Query referenced tables through a join table')

We can infer types by doing this:

// To infer types, use the name of the table (in this case `users`) and
// the name of the foreign key constraint.
const { data, error } = await supabase
  .from('messages')
  .select(`
    content,
    from:users!messages_sender_id_fkey(name),
    to:users!messages_receiver_id_fkey(name)
  `)

Worked for me.

rafal-r avatar Sep 24 '24 20:09 rafal-r