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

关联查询中如何排除USER表数据,仅返回PROFILE与COMPANY信息

解决Prisma查询中返回多余USER表字段的问题

修改后的Prisma查询语句如下:

const proponents = count > 0 ? await client.proponent.findMany({
  select: {
    id: true,
    organizationName: true,
    user: {
      select: { // 将include替换为select
        company: {
          select:{
            name: true,
            phoneCompany: true,
            registeredOffice: true,
            vatNumber: true,
            websiteUrl: true,
            description: true,
            shareCapital: true,
          },
        },
        profile:{
          select: {
            firstName: true,
            lastName: true,
          },
        },
        // 不声明任何USER表自身字段,就不会返回这类信息
      },
    },
  },
  ...pagination,
  where: { ...prismaWhereProponents },
}) : [];

原因说明

你之前在user字段下使用include时,Prisma会默认返回USER表的所有自身字段,再附加指定的关联数据。改用select且不指定USER表的任何自身字段,仅配置需要关联的company和profile的查询规则,就能确保USER表的createdAt、email等多余信息不会被返回,同时保留需要的关联字段数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 04:20:02