我正试图在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[]