Prisma不能更新抛出类型错误,尽管在findMany上使用了相同的类型



我正在尝试更新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; }'

试试这个:

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

修复方法是在模式中的paymentId中添加一个@unique。如果你按1列搜索,它需要是唯一的列在上传等

最新更新