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

Prisma Upsert操作中可选ID处理的排障请求

问题分析与解决方案

可能原因

1. Swagger Schema生成异常

你在DTO里给id标记了@ApiProperty({ required: false })和TypeScript可选符?,但NestJS Swagger插件有时会对可选字段识别不彻底,生成的OpenAPI Schema里仍把id设为必填,导致Swagger UI校验时抛出"Missing field"错误。另外如果没给id加类型校验,传入非字符串值时会触发"invalid field type"错误。

2. Prisma Upsert逻辑存在隐患

当不传id时,你的Repository里upsert的where条件是{ id: undefined },Prisma找不到匹配记录就会执行create,但vendor是唯一字段,若该vendor已存在会直接触发数据库唯一约束报错——这虽然不是当前Swagger报错的直接原因,但必须修复避免后续业务异常。

解决步骤

1. 修正Swagger字段配置

修改DTO中id的@ApiProperty,显式声明nullable: true和类型,同时补上字符串校验,确保Swagger正确识别可选性:

@ApiProperty({ 
  example: '1', 
  required: false,
  nullable: true,
  type: String
})
@IsOptional()
@IsString() // 强制校验传入值为字符串类型
id?: string;

2. 调整Upsert业务逻辑

结合vendor的唯一约束,优化upsert逻辑:有id就按id匹配更新,没id就按vendor判断是更新已有记录还是创建新记录:

async upsert({ vendor, id }: VendorDto) {
  const whereCondition = id ? { id } : { vendor };
  return this.prisma.vendor.upsert({
    where: whereCondition,
    update: { vendor },
    create: id ? { vendor, id } : { vendor },
  });
}

3. 确保DTO转换生效

检查main.ts里的ValidationPipe配置,开启transform: true让NestJS自动把请求体转成DTO实例,避免类型不匹配:

// main.ts
app.useGlobalPipes(new ValidationPipe({
  transform: true,
  whitelist: true, // 过滤多余字段,避免干扰校验
}));

4. 刷新Swagger缓存

修改后重启NestJS服务,清除浏览器缓存再打开Swagger UI,确保新的API Schema生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:03:08