skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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);
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search