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

Prisma NodeJS:如何在查询中同时包含Foo和Bar的ID及所有字段?

解决方案

你可以使用Prisma的include选项来加载关联的Bar模型,同时默认返回Foo和Bar的所有字段,完全不需要手动用select列举字段。

替换???的内容如下:

{
  include: {
    bar: true
  }
}

完整的代码示例:

import { PrismaClient } from "@prisma/client";

async function main() {
   let client = new PrismaClient();
   let foo = await client.foo.findFirst({
     include: {
       bar: true
     }
   });
   console.log(foo);
};

main()

执行后返回的foo对象会包含Foo模型的所有字段(id、name),以及关联的Bar对象(如果存在的话,包含它的id、name、fooId等所有字段)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 21:20:24