关联查询中如何排除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
相关产品推荐
相关产品推荐

