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

Prisma更新同层级字段丢失:如何保留oldState更新currentState

Prisma嵌套对象更新保留原有字段解决方案

你当前的更新逻辑会直接替换整个sports对象,导致oldState字段被删除。这是因为你在data字段下使用update时,是对整个SourcesData对象进行操作,而非针对嵌套的sports子对象做部分更新。

要保留oldState数据,只需调整嵌套更新的语法,直接针对sports下的子字段进行修改:

const updatedSource = await prisma.sources.update({
  where: {
    name: 'toy'
  },
  data: {
    data: {
      sports: {
        update: {
          currentState: "true"
        }
      }
    }
  },
})

语法说明

这里的update是作用在SourcesDataState类型的sports对象上,只会修改指定的currentState字段,原有的oldState会被完整保留。

如果你的Prisma版本支持简化语法,也可以直接传入要更新的字段,Prisma会自动处理部分更新:

const updatedSource = await prisma.sources.update({
  where: {
    name: 'toy'
  },
  data: {
    data: {
      sports: {
        currentState: "true"
      }
    }
  },
})

两种写法都能实现保留oldState的同时更新currentState,显式使用update语法在复杂嵌套场景下更清晰不易出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 18:20:42