在Supabase中查询public schema时能否关联storage schema数据?
问题:Supabase关联查询product与storage.objects返回null
场景说明
我有一张product表,其中product_image字段与storage.objects.id存在外键关联。已将存储桶设为公开,且配置了RLS策略允许查询storage schema的所有数据,但关联查询始终返回null。
尝试过的查询写法
// 写法1 supabase.from("category_display") .select(product(*, storage.objects(*))); // 写法2 supabase.from("category_display") .select(product(*, objects(*))); // 写法3 supabase.from("product").select(`*, product_images(*) <-- bucket name `);
单独查询product表的正常结果
执行以下查询:
supabase.from("product").select(`*`);
返回正常数据:
[ { "id": "8889b965-7992-477d-bc47-d6460f0a972a", "name": "Diary milk chocolate", "desc": null, "sku": null, "category_id": "bbb72ada-d332-429d-9aba-f881c5f6eca7", "inventory_id": null, "price": 50, "discount_id": null, "deleted_at": "2023-06-30T03:55:11.170276+00:00", "updated_by": null, "updated_at": "2023-07-03T02:27:35.231514+00:00", "created_at": "2023-06-30T03:55:11.170276+00:00", "per": "Units", "product_image": "1162a110-c28e-47dd-89b4-40ca393ce3f0" <--- 关联storage.objects.id的字段 } ]
正确查询方式
在Supabase中关联跨schema的storage.objects表时,需要遵循正确的关联语法,以下是可行的写法:
直接查询product并关联storage.objects
supabase.from("product") .select(`*, storage.objects(*)`)
只要外键关联已正确设置,Supabase会自动匹配product.product_image与storage.objects.id的关联关系。
从category_display视图关联查询
如果要从category_display视图关联product再拉取storage数据:
supabase.from("category_display") .select(`product(*, storage.objects(*))`)
额外排查点
- 确认
product.product_image字段和storage.objects.id字段的类型完全匹配(均为UUID) - 检查
storage.objects表中是否存在对应product_image值的记录 - 确保RLS策略同时允许对
product表和storage.objects表的查询操作
内容的提问来源于stack exchange,提问作者Mohammed Yasin Mulla
相关产品推荐
相关产品推荐

