Prisma create not connecting relation, instead wants normal fields

Viewed 106

prisma: 4.0.0 @prisma/client: 4.0.0

When trying to insert data into the table I get this error specifically when creating many entrega objects, but not when creating many nota objects:

Invalid `prisma.entrega.createMany()` invocation:

{
  data: {
    '0': {
      Mes: 8,
      Ano: 22,
      Peso: 656.4484,
      Saldo: 6541160.6874,
      Tela: {
      ~~~~
        connect: {
          ID: 1
        }
      },
      Contrato: {
      ~~~~~~~~
        connect: {
          ID: 2
        }
      },
+     IDContrato: Int,
+     IDTela: Int
    },
    '1': {
      Mes: 7,
      Ano: 44,
      Peso: 4549.81,
      Saldo: 33241.544,
      Tela: {
      ~~~~
        connect: {
          ID: 2
        }
      },
      Contrato: {
      ~~~~~~~~
        connect: {
          ID: 2
        }
      },
+     IDContrato: Int,
+     IDTela: Int
    }
  }
}

Unknown arg `Tela` in data.0.Tela for type EntregaCreateManyInput. Did you mean `IDTela`?
Unknown arg `Contrato` in data.0.Contrato for type EntregaCreateManyInput. Did you mean `IDContrato`?
Argument IDContrato for data.0.IDContrato is missing.
Argument IDTela for data.0.IDTela is missing.
Unknown arg `Tela` in data.1.Tela for type EntregaCreateManyInput. Did you mean `IDTela`?
Unknown arg `Contrato` in data.1.Contrato for type EntregaCreateManyInput. Did you mean `IDContrato`?
Argument IDContrato for data.1.IDContrato is missing.
Argument IDTela for data.1.IDTela is missing.

Here are the models, they are pretty much alike:

model Entrega {
  ID         Int         @id @default(autoincrement())
  IDContrato Int
  IDTela     Int
  Mes        Int?
  Ano        Int?
  Peso       Decimal?    @db.Decimal(18, 4)
  Saldo      Decimal?    @db.Decimal(18, 4)
  Contrato   Contrato    @relation(fields: [IDContrato], references: [ID], onUpdate: NoAction)
  Tela       Tela        @relation(fields: [IDTela], references: [ID], onUpdate: NoAction)
  Instrucao  Instrucao[]

  @@map("U_Entrega")
}

model Nota {
  ID          Int       @id @default(autoincrement())
  TipoDoc     String?   @db.Char(1)
  Numero      String?   @db.NVarChar(255)
  Data        DateTime? @db.DateTime
  Peso        Decimal?  @db.Decimal(18, 4)
  Fardos      Decimal?  @db.Decimal(18, 4)
  VlrUnitNota Decimal?  @db.Decimal(18, 4)
  DolarDia    Decimal?  @db.Decimal(18, 4)
  IDLote      Int?
  Lote        Lote?     @relation(fields: [IDLote], references: [ID], onDelete: NoAction, onUpdate: NoAction)

  @@map("U_Nota")
}

These are the services for creating them

CreateEntregasService:

import { Prisma } from '@prisma/client'

import { prismaClient } from '../../common/services/prisma'

class CreateEntregasService {
  async execute(Entregas: any[], contractId: number) {
    
    const entregas = Entregas.map((entrega) => {
      const { id, IDTela, ...entregaData } = entrega

      const newEntrega: Prisma.EntregaCreateInput = {
        ...entregaData,
        Tela: {
          connect: {
            ID: IDTela,
          },
        },
        Contrato: {
          connect: {
            ID: contractId,
          },
        },
      }

      return newEntrega
    })

    console.log(entregas)
    try {
      await prismaClient.entrega.createMany({
        data: [...entregas],
      })

      return 'Entregas criadas com sucesso'
    } catch (e) {
      console.log(e)
      throw new Error('Não foi possível criar as entregas')
    }
  }
}

export { CreateEntregasService }

CreateNotasService

import { Prisma } from '@prisma/client'
import { prismaClient } from '../../common/services/prisma'

class CreateNotasService {
  async execute(Notas: any[]) {
    const notas = Notas.map((nota) => {
      const { id, IDLote, ...notasData } = nota

      const newNota: Prisma.NotaCreateInput = {
        ...notasData,
        Lote: {
          connect: {
            ID: IDLote,
          },
        },
      }

      return newNota
    })

    try {
      await prismaClient.nota.createMany({
        data: [...notas],
      })

      return 'Notas criadas com sucesso'
    } catch (e) {
      throw new Error('Erro ao criar as notas')
    }
  }
}

export { CreateNotasService }

My VSCode also gives me this typing error in the CreateEntregasService:

Type 'EntregaCreateInput[]' is not assignable to type 'Enumerable<EntregaCreateManyInput>'.
  Type 'EntregaCreateInput[]' is not assignable to type 'EntregaCreateManyInput[]'.
    Type 'EntregaCreateInput' is missing the following properties from type 'EntregaCreateManyInput': IDContrato, IDTela

I cannot figure the problem at all.

0 Answers
Related