设置数组时缺少所需的值

Missing a required value when setting array

我正在使用 Prisma upsert 并在尝试设置其中一个字段时收到有关缺少必需值的错误,该字段是数组类型,特别是 numeric[].

我使用的是 Prisma 2.6.0 版

除了少数几行之外,所有数据都进入了数据库,当更新插入执行时,我收到了 Prisma 在这些行上抛出的错误。

我从 prisma 得到的错误与我试图设置的数组有关:

PrismaClientKnownRequestError: 
Invalid `prisma.node.upsert()` invocation in
/usr/development/project/dist/project-tsdx.cjs.development.js:604:46


  Missing a required value at `Mutation.upsertOnenode.create.nodeCreateInput.temperature.nodeCreatetemperatureInput.set`
    at PrismaClientFetcher.request (/usr/development/project/node_modules/@prisma/client/runtime/index.js:1:227598)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Promise.all (index 3366) {
  code: 'P2012',
  meta: {
    path: 'Mutation.upsertOnenode.create.nodeCreateInput.temperature.nodeCreatetemperatureInput.set'
  }
}

发生异常的对象有以下值:

threw this node: 
Node {
  name: 'ahostname',
  state: 'NONE',
  cores: -1,
  busycpus: 0,
  queue: 'NONE',
  rack: 'NONE',
  jobs: '',
  temperature: [ -1 ],
  tempupdatetime: '1970-01-01T00:00:00.000Z',
  power: 0,
  powerupdatetime: '1970-01-01T00:00:00.000Z',
  uptime: 20,
  loadavg1: 0,
  loadavg5: 0.01,
  loadavg15: 0.05,
  cpuusage: 0.2,
  updatetime: '2020-09-01T17:29:02.000Z'
}

我的 Node 模型架构如下:

model node {
  node            String   @id
  state           String
  cores           Int
  busycpus        Int
  queue           String
  rack            String
  jobs            String
  temperature     Float[]
  tempupdatetime  DateTime
  power           Float
  powerupdatetime DateTime
  uptime          Int
  loadavg1        Float
  loadavg5        Float
  loadavg15       Float
  cpuusage        Float
  updatetime      DateTime
}

最后,我用来设置这个数组的实际代码如下:

import { PrismaClient } from '@prisma/client'
import { NodeStats } from '../sources/nodeStats'
const prisma = new PrismaClient()

export default async (nodes: NodeStats) => {
  const { timestamp, ...others } = nodes
  //@ts-ignore
  let currentNode
  const promises = Object.keys(others).map(async nodeName => {
    try {
      const node = nodes[nodeName]
      currentNode = node
      let { temperature, name, ...others } = node
      
      if (temperature.length === 1 && temperature[0] === 0){
        node.temperature = [-1]
        temperature = [-1]
      }

      // if(!temperature) console.log(currentNode)
      await prisma.node.upsert({
        create: { ...others, node: name, temperature: {set: [...temperature]}},
        where: { node: nodeName },
        update: { ...others, node: name, temperature: { set: [...temperature] } },
      })
    } catch (e) {
      console.error("threw this node:", currentNode)
       console.error(e)
    }
  })
  return Promise.all(promises)
}

所有被抛出的 Node 都有一个共同点,它们的 temperature 值为 [0]。我试图将其更改为 [-1] 但这并没有阻止错误的发生。真的对这里发生的事情感到困惑,任何提示将不胜感激。

我写了一些玩笑测试来确定造成这种情况的原因,结果证明这是由传入 [0, null] 引起的。修复数据集后,这个问题就解决了。