admin管理员组文章数量:1414621
I have 2 tables in supabase. We have a post table
and an image
table. Each post contains multiple images. In my image
table, I have the post_id
and url
. The post_id
is a foreign key to post
's id.
Post Table:
| id | contents |
| -------- | -------------- |
| 1 | some content 1 |
| 2 | some content 2 |
Image Table:
| id | url | post_id|
| -------- | ------------------- | ------ |
| 10 | url2 | 1 |
| 11 | url1 | 2 |
| 12 | url3 | 2 |
I want my output to look like:
[
{
"id": 1,
"content": "some content 1"
"images": [
"url2"
]
},
{
"id": 2,
"content": "some content 2"
"images": [
"url1",
"url3"
]
}
]
My fetch request looks something like this:
const fetchPosts = async (start, end) => {
console.log(`Fetching all posts...`);
return await supabase
.getClient()
.from('post')
.select('*')
.order('inserted_at', { ascending: false })
.range(start, end);
}
and then I'm fetching images using each post id from that query. Is there away for me to just use one supabase query instead of looping through each post and fetching what images are linked to that post?
I have 2 tables in supabase. We have a post table
and an image
table. Each post contains multiple images. In my image
table, I have the post_id
and url
. The post_id
is a foreign key to post
's id.
Post Table:
| id | contents |
| -------- | -------------- |
| 1 | some content 1 |
| 2 | some content 2 |
Image Table:
| id | url | post_id|
| -------- | ------------------- | ------ |
| 10 | url2. | 1 |
| 11 | url1. | 2 |
| 12 | url3. | 2 |
I want my output to look like:
[
{
"id": 1,
"content": "some content 1"
"images": [
"url2."
]
},
{
"id": 2,
"content": "some content 2"
"images": [
"url1.",
"url3."
]
}
]
My fetch request looks something like this:
const fetchPosts = async (start, end) => {
console.log(`Fetching all posts...`);
return await supabase
.getClient()
.from('post')
.select('*')
.order('inserted_at', { ascending: false })
.range(start, end);
}
and then I'm fetching images using each post id from that query. Is there away for me to just use one supabase query instead of looping through each post and fetching what images are linked to that post?
Share Improve this question edited Jan 25, 2023 at 13:48 Mansueli 7,0448 gold badges40 silver badges67 bronze badges asked Jan 24, 2023 at 16:06 Misu KumaMisu Kuma 1252 silver badges11 bronze badges1 Answer
Reset to default 4You can use JOIN queries using Supabase, e.g:
const { data, error } = await supabase
.from('image')
.select(`
id, url, post_id
post (
content
)
`)
Depending on their plexity, you can also encapsulate these calls in RPC functions.
For joins in multiple tables, then you can do the following:
const { data, error } = await supabase
.from('products')
.select(`
id,
supplier:supplier_id ( name ),
purchaser:purchaser_id ( name )
`)
It is important to set the Foreign key as part of your primary key as part of the way PostgREST v10 detects relationships.
Image Table Primary key should be:
alter table image drop constraint image_pkey, add primary key (id, post_id);
本文标签: javascriptQuery from multiple tables in supabaseStack Overflow
版权声明:本文标题:javascript - Query from multiple tables in supabase? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745191140a2646915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论