0

I'm using Prisma upsert and getting an error about missing a required value when I'm trying to set one of the fields, which is an array type, specifically numeric[].

I'm using Prisma version 2.6.0

Data is getting into the database for all except a handful of rows and I'm getting an error thrown by Prisma on those rows when the upsert executes.

The error I'm getting from prisma is related to the array I'm trying to set:

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'
  }
}

The object that the exception occurs on has the following values:

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'
}

My schema for the Node model is as follows:

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
}

Finally, the actual code that I'm using to set this array is below:

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)
}

All of the Nodes that get thrown have one thing in common, their temperature value is [0]. I tried to change it to [-1] but that didn't stop the error from occurring. Really puzzled on what is going on here, any tips would be appreciated.

giraffesyo
  • 4,860
  • 1
  • 29
  • 39
  • Instead of `temperature: { set : [...] }` can you try `temperature: [...]`? Basically remove the outer block surronding `set` and the `set` itself. – oneturkmen Sep 01 '20 at 18:10
  • @oneturkmen Thanks for your reply! In that case TS red underlines temperature with the following `Type 'number[]' has no properties in common with type 'nodeUpdatetemperatureInput'.ts(2559)` – giraffesyo Sep 01 '20 at 19:17
  • Hmm. Maybe keep `set` in the `update` field, but remove it from the `create` field? It looks like it tries to *create* a new variable with field `set`, which does not exist. – oneturkmen Sep 01 '20 at 19:23
  • @oneturkmen Unfortunately it doesn't like it if it's removed from either location. Although temperature itself is optional. – giraffesyo Sep 01 '20 at 19:37

1 Answers1

0

I wrote some jest tests to determine the cause of this and it turns out this was caused by passing in [0, null]. After fixing the dataset this issue cleared up.

giraffesyo
  • 4,860
  • 1
  • 29
  • 39