我正试图在posts表中查询一个帖子,其中包含用户的user_id,我想使用user_id将其与profiles表连接起来.

POST表模式:(post_id: uuid, created_at: timestamptz, image_url: text, user_id: uuid, num_likes: integer, description: text)我的外键关系是用这个表和user_id的配置文件在SuperBase上建立的.

配置文件表模式:(user_name: text, first_name: text, last_name: text, is_premium: boolean, user_id: uuid, num_posts: smallint, num_followers: integer, num_following: integer)

try 使用以下命令查询表时:

const { data, error } = await supabase
      .from("posts")
      .select(
        `post_id, created_at, image_url, user_id, num_likes, description, profiles ( user_id, user_name, first_name, last_name, num_followers, num_following, num_posts, is_premium )`
      )
      .limit(1)
      .single();

我没有收到任何错误,并且我正确地接收到了用户的配置文件信息,但data.profiles应该是一个array.然而,它不返回数组,它只返回一个data.profiles的对象.

查询返回的示例:

Object {
  "created_at": "2023-07-29T15:54:53.849017+00:00",
  "description": "This is a description",
  "image_url": "NULL",
  "num_likes": 3,
  "post_id": "e0392738-5085-4026-9446-56e4bde69fee",
  "profiles": Object {
    "first_name": "John",
    "is_premium": true,
    "last_name": "Doe",
    "num_followers": 0,
    "num_following": 0,
    "num_posts": 0,
    "user_id": "108a1e53-1df6-4baf-a4ca-f0c445f085d2",
    "user_name": "User123",
  },
  "user_id": "108a1e53-1df6-4baf-a4ca-f0c445f085d2",
}

但这是预期的类型:

data: {
    post_id: any;
    created_at: any;
    image_url: any;
    user_id: any;
    num_likes: any;
    description: any;
    profiles: {
        user_id: any;
        user_name: any;
        first_name: any;
        last_name: any;
        num_followers: any;
        num_following: any;
        num_posts: any;
        is_premium: any;
    }[];
}

更多背景信息: 下面是从suabase调用的未经编辑的函数:

export const getNextPost = createAsyncThunk(
  "getNextPost",
  async (payload: {
    lastPostCreatedAt: string;
  }): Promise<(postsTableRow & { profiles: profilesTableRow }) | null> => {
    const { data, error } = await supabase
      .from("posts")
      .select(
        `post_id, created_at, image_url, user_id, num_likes, description, profiles ( user_id, user_name, first_name, last_name, num_followers, num_following, num_posts, is_premium )`
      )
      .order("created_at", { ascending: false })
      .lt("created_at", payload.lastPostCreatedAt)
      .limit(1)
      .single();
    if (error?.code === "PGRST116") return null;
    if (error) console.warn(error);
    console.log("DATA:", data);
    return data as postsTableRow & { profiles: profilesTableRow };
  }
);

如果我将返回类型从:Promise<(postsTableRow & { profiles: profilesTableRow }) | null>更改为 至Promise<(postsTableRow & { profiles: profilesTableRow[] }) | null>

编译器警告消失,但仍返回profilesTableRow,而不是预期的profilesTableRow[]

推荐答案

我找到了一个更好的解决方案: 似乎为超数据库客户端生成类型,使用超数据库CLI似乎也修复了这个问题.Supabase Docs:https://supabase.com/docs/guides/api/rest/generating-types

Typescript相关问答推荐

有没有办法适当缩小类型?

有没有一种方法可以在保持类型的可选状态的同时更改类型?

基于平台的重定向,Angular 为16

有没有办法解决这个问题,类型和IntrinsicAttributes类型没有相同的属性?

使用泛型keyof索引类型以在if-condition内类型推断

仅针对某些状态代码重试HTTP请求

带switch 的函数的返回类型的类型缩小

如何在Reaction路由v6.4加载器和操作中获得Auth0访问令牌?

如何调整对象 struct 复杂的脚本函数的泛型类型?

在对象中将缺省值设置为空值

使用KeyOf和Never的循环引用

从子类集合实例化类,同时保持对Typescript中静态成员的访问

使用条件类型的类型保护

在类型脚本中将泛型字符串数组转换为字符串字母并集

如何在TypeScript中将元组与泛型一起使用?

通过辅助函数获取嵌套属性时保留类型

断言同级为true或抛出错误的Typescript函数

在类型{}上找不到带有string类型参数的索引签名 - TypeScript

我可以使用对象属性的名称作为对象中的字符串值吗?

如何使用布尔值构建对象 TYPE,但至少有一个必须为 true