gpt4 book ai didi

reactjs - 下一个带有 Prisma 的 js : Upsert based on two conditions

转载 作者:行者123 更新时间:2023-12-05 01:55:43 25 4
gpt4 key购买 nike

我正在尝试使用 Prisma 执行以下操作:

如果存在具有相同散列和 user_id 的类别行,只需更新它的“名称”字段,否则,创建该行

这可能吗? TS 给我一个错误,说“where”上给出的键的类型必须是 categoriesWhereUniqueInput,但是 hash 和 user_id 都不是唯一的,它们可以重复,这是两者之间的组合将是独一无二的

我该如何解决这个问题?我是否必须手动检查是否有 id 并根据它更新/创建?

提前致谢!

  const category = await prisma.categories.upsert({
where: {
hash,
user_id: id,
},
update: {
name,
},
create: {
hash,
name,
user_id: id,
},
});

最佳答案

当字段组合是唯一的时,Prisma 将为 where 条件生成一个新类型,它基本上只是所有相关字段的名称加上 _ .

查看 categoriesWhereUniqueInput 以了解其名称。它很可能被称为 user_id_hashhash_user_id

这就是查询的样子,粗略地说:

const category = await prisma.categories.upsert({
where: {
user_id_hash: { // user_id_hash is the type generated by Prisma. Might be called something else though.
user_id: 1,
hash: "foo"
}
},
update: {
name: "bar",
},
create: {
hash: "foo",
name: "bar",
user_id: 1,
},
})

关于reactjs - 下一个带有 Prisma 的 js : Upsert based on two conditions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70116616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com