updateMany 找不到参数
Posted
技术标签:
【中文标题】updateMany 找不到参数【英文标题】:updateMany can't find argument 【发布时间】:2021-11-19 04:42:29 【问题描述】:我有一个问题:
createNotification: async (_, args, req, res) =>
const followedBy = await prisma.user.updateMany(
where:
following:
some:
id: req.userId
,
,
,
data:
notifications:
create:
message: args.message,
watched: false,
,
,
,
)
以及用户和通知模型:
model User
id Int @id @default(autoincrement())
email String @unique
name String
user_name String @unique
password String
movies Movie[]
notifications Notification[]
followedBy User[] @relation("UserFollows", references: [id])
following User[] @relation("UserFollows", references: [id])
model Notification
id Int @id @default(autoincrement())
link String?
movie_id Int?
message String
icon String?
thumbnail String?
user User @relation(fields: [userId], references: [id])
userId Int
watched Boolean
运行查询时出现错误:
Unknown arg `notifications` in data.notifications for type UserUpdateManyMutationInput. Did you mean `email`? Available args:
type UserUpdateManyMutationInput
email?: String | StringFieldUpdateOperationsInput
name?: String | StringFieldUpdateOperationsInput
user_name?: String | StringFieldUpdateOperationsInput
password?: String | StringFieldUpdateOperationsInput
奇怪的是这行得通:
const followedBy = await prisma.user.findUnique(
where: id: req.userId,
include:
followedBy: true,
,
);
followedBy.followedBy.map(async(user) =>
await prisma.user.update(
where: id: user.id,
data:
notifications:
create:
message: args.message,
watched: false,
,
,
,
);
);
但这并没有充分利用 Prisma 提供的功能。
【问题讨论】:
【参考方案1】:截至 2021 年 9 月,Prisma 不支持在*** updateMany
查询中更改嵌套关系。这就是打字稿错误试图告诉您的内容,您只能访问data
中的email
、name
、user_name
和password
字段。有一个开放的feature request for this,如果您有兴趣,可以关注它。
对于您提供的架构,这里有一个可能的解决方法,它的可读性略低,但比您当前的解决方案更优化。
createNotification: async (_, args, req, res) =>
// get the followers array of req.userId
const followedBy = await prisma.user.findUnique(
where: id: req.userId ,
include:
followedBy: true,
,
);
// array of notification objects to be created, one for each follower of req.userId
let messageDataArray = followedBy.followedBy.map((user) =>
return
userId: user.id,
message: args.message,
watched: false,
;
);
// do a bulk createMany.
// Since it's one query, it should be more optimized than running an update for each user in a loop.
await prisma.notification.createMany(
data: messageDataArray,
);
;
如果您有兴趣,请here's the docs reference 了解可能的嵌套更新类型。
【讨论】:
以上是关于updateMany 找不到参数的主要内容,如果未能解决你的问题,请参考以下文章
Gradle build:找不到参数的方法signingConfig()