英文:
How to pull data from multi tables at once from supabase tables?
问题
我在supabase数据库上有一些表格。<br />
有一个名为post的表格,其中包含来自profiles表的user_id。<br />user_id
字段与profiles表的id相关联。<br />
我正在使用@supabase
节点模块从supabase中提取数据。<br />我可以使用单个查询提取数据,但我不知道如何使用一个查询从多个表格中提取数据。<br />
我按照这个答案的方法,但它对我没有帮助。响应的结果不包含个人资料信息。
<br />以下是我尝试使用的代码。
await supabase
.from('posts')
.select(`user_id,
profiles(
first_name
)
`);
我该如何解决这个问题?
英文:
I have some tables on supabase db. <br />
There is a post table and it contains user_id from profiles table. <br /> user_id
field related with the id of profiles table. <br />
I am using @supabase
node module for pulling data from supabase. <br /> I can pull data with single query, but I don't know the way of pulling data from multiple tables with a query.<br />
I followed this answer, but it doesn't help me. The response's result doesn't contain the profile information.
<br />Here is the code I tried to use.
await supabase
.from('posts')
.select(`user_id,
profiles(
first_name
)
`);
How can I solve this problem?
答案1
得分: 0
假设您的 user_id
列与 profiles
表之间存在外键关系,以下代码将为您返回每个帖子行上的用户 ID 的帖子数据以及个人资料数据。
const { data, error } = await supabase
.from('posts')
.select(`*,
profiles(
first_name
)
`);
如果这不起作用,您可能没有在 user_id
列和 profiles
表之间建立外键关系。您可以阅读官方指南,了解如何添加外键关系。
https://supabase.com/docs/guides/database/tables#joining-tables-with-foreign-keys
英文:
Assuming you have a foreign key relationship between your user_id
column and the profiles
table, the following will return you the posts data as well as the profiles data of user_id on each posts row.
const { data, error } = await supabase
.from('posts')
.select(`*,
profiles(
first_name
)
`);
If this does not work, you probably do not have a foreign key relationship between the user_id
column and profiles
table. You can read the official guide on how to add foreign key relationship.
https://supabase.com/docs/guides/database/tables#joining-tables-with-foreign-keys
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论