Home > OS >  Prisma cannot update throws type error despite using same exact type on a findMany
Prisma cannot update throws type error despite using same exact type on a findMany

Time:01-03

I am trying to update a record in prisma and it will not let me query the record with an update. I use the exact same where condition for both a findMany and the update but the update does not work. See error below for more details.

            const transaction = await prisma.coinTransaction.findMany({
                where: {
                    paymentId: paymentIntent.id
                },
                select: {
                    paymentId: true
                }
            });
            if (transaction.length > 1) {
                console.log('Error not unique')
            } else {
                console.log('transaction: ', transaction[0])
                await prisma.coinTransaction.update({
                    where: {
                        paymentId: paymentIntent.id
                    },
                    data: {
                        checkoutSessionCompleted: new Date()
                    }
                })
            }

Error in vscode

Type '{ paymentId: any; }' is not assignable to type 'CoinTransactionWhereUniqueInput'.
  Object literal may only specify known properties, and 'paymentId' does not exist in type 'CoinTransactionWhereUniqueInput'.ts(2322)
index.d.ts(11553, 5): The expected type comes from property 'where' which is declared here on type '{ select?: CoinTransactionSelect | null | undefined; include?: CoinTransactionInclude | null | undefined; data: (Without<CoinTransactionUpdateInput, CoinTransactionUncheckedUpdateInput> & CoinTransactionUncheckedUpdateInput) | (Without<...> & CoinTransactionUpdateInput); where: CoinTransactionWhereUniqueInput; }'

CodePudding user response:

Try this:

try {
  const transaction = await prisma.coinTransaction.findUniqueOrThrow({
    where: {
      paymentId: paymentIntent.id, // <--- assuming that this is unique! 
    },
    select: {
      paymentId: true,
    },
  });

  console.log('transaction: ', transaction);
  await prisma.coinTransaction.update({
    where: {
      paymentId: transaction.paymentId,
    },
    data: {
      checkoutSessionCompleted: new Date(),
    },
  });
} catch (error) {
  console.log('error: ', error);
}

CodePudding user response:

The fix was to add a @unique to the paymentId in the schema. If you search by 1 column it needs to be a unique column in upload etc.

  • Related