javascript 尽管在findMany上使用了完全相同的类型,但Prisma无法更新throws类型错误

n53p2ov0  于 2023-01-01  发布在  Java
关注(0)|答案(2)|浏览(116)

我正在尝试更新prisma中的记录,但它不允许我使用更新查询记录。我对findMany和更新使用了完全相同的where条件,但更新不起作用。有关详细信息,请参阅下面的错误。

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

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

xqkwcwgp1#

试试这个:

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

vltsax252#

修复方法是将@unique添加到架构中的paymentId。如果按1列搜索,则该列在上载等操作中必须是唯一的列。

相关问题