You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Supabase访问二级外键关联数据

解决方法

你的问题出在嵌套查询的关联字段写法上——你用了表名countries,但应该用cities表中关联countries的外键字段名country。

修改后的代码如下:

async function fetchJobs() {
  const { data: fetchedJobs } = await supabaseInstance()
    .from("jobs")
    // 用外键字段名country关联countries表,获取所有字段
    .select("*, cities!inner(*, country!inner(*))")
    .order('created_at', { ascending: false });
  setJobs(fetchedJobs)
}

如果只需要country的特定字段(比如id和名称),可以指定字段列表:

.select("*, cities!inner(*, country!inner(id, name))")

这样修改后,你就能通过job.cities.country访问到对应的国家数据了。

内容的提问来源于stack exchange,提问作者Ahmed Wagdi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 09:25:07