postgresql - Supabase self-referencing one-to-one relationship returns array instead of object - Stack Overflow

I created a profiles table, where creator_id has a self-referencing one-to-one relationship. Since the

I created a profiles table, where creator_id has a self-referencing one-to-one relationship. Since the referenced id column is unique, I expect the returned row to be an object.

create table public.profiles (
  id uuid not null,
  creator_id uuid not null default auth.uid (),
  first_name text not null,
  last_name text not null,
  constraint profiles_pkey primary key (id),
  constraint profiles_slug_key unique (slug),
  constraint profiles_id_key unique (id),
  constraint profiles_id_fkey foreign KEY (id) references auth.users (id) on delete CASCADE,
  constraint profiles_creator_id_fkey foreign KEY (creator_id) references profiles (id)
) TABLESPACE pg_default;
const { data, error } = await supabase.from('profiles').select(
  '*, creator:profiles!creator_id(id, first_name, last_name)'
)

Instead, the creator is returned as an array of objects:

{
  "id": "c2b63d53-30a9-4bca-9a41-d769e8d0a8ba",
  "first_name": "Hannah",
  "last_name": "Montana",
  "creator_id": "cd6e8ef1-2517-4f55-bfea-ba2dd81e4c55",
  "creator": [
    {
      "id": "cd6e8ef1-2517-4f55-bfea-ba2dd81e4c55",
      "last_name": "Sterner",
      "first_name": "Werner"
    }
  ]
}

I created a profiles table, where creator_id has a self-referencing one-to-one relationship. Since the referenced id column is unique, I expect the returned row to be an object.

create table public.profiles (
  id uuid not null,
  creator_id uuid not null default auth.uid (),
  first_name text not null,
  last_name text not null,
  constraint profiles_pkey primary key (id),
  constraint profiles_slug_key unique (slug),
  constraint profiles_id_key unique (id),
  constraint profiles_id_fkey foreign KEY (id) references auth.users (id) on delete CASCADE,
  constraint profiles_creator_id_fkey foreign KEY (creator_id) references profiles (id)
) TABLESPACE pg_default;
const { data, error } = await supabase.from('profiles').select(
  '*, creator:profiles!creator_id(id, first_name, last_name)'
)

Instead, the creator is returned as an array of objects:

{
  "id": "c2b63d53-30a9-4bca-9a41-d769e8d0a8ba",
  "first_name": "Hannah",
  "last_name": "Montana",
  "creator_id": "cd6e8ef1-2517-4f55-bfea-ba2dd81e4c55",
  "creator": [
    {
      "id": "cd6e8ef1-2517-4f55-bfea-ba2dd81e4c55",
      "last_name": "Sterner",
      "first_name": "Werner"
    }
  ]
}
Share Improve this question asked Mar 25 at 10:49 peperolipeperoli 1095 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

As it seems, it's not necessary to specify the table name with !profiles if self-referencing. When using '*, creator:creator_id(id, first_name, last_name)', it works as expected.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744202432a4562943.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信