javascript - Query from multiple tables in supabase? - Stack Overflow

I have 2 tables in supabase. We have a post table and an image table. Each post contains multiple image

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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

You 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);

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

相关推荐

  • javascript - Query from multiple tables in supabase? - Stack Overflow

    I have 2 tables in supabase. We have a post table and an image table. Each post contains multiple image

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信