Prisma嵌套创建关联时提示未知参数profile的问题排查
问题原因及解决办法
报错原因
你在创建CommunicationChannel时多余传入了profile参数。因为你是在prisma.profile.update的上下文里通过channels.create创建关联记录,Prisma会自动将当前更新的Profile(即where条件指定的id: myId的Profile)与新生成的CommunicationChannel绑定,不需要手动通过profile.connect重复指定父级关联。这种重复指定违反了Prisma的验证规则,所以抛出Unknown argument 'profile'错误。
修正代码
移除create对象中的profile字段即可正常运行:
await prisma.profile.update({ where: { id: myId }, include: { channels: true }, data: { channels: { create: [ { job: { connect: { id: jobId } }, }, ], }, }, });
特殊场景补充
如果你的目标是创建CommunicationChannel关联到其他Profile(而非当前更新的Profile),不要通过当前Profile的channels关联创建,直接调用communicationChannel.create即可:
await prisma.communicationChannel.create({ data: { profile: { connect: { id: targetProfileId } }, job: { connect: { id: jobId } }, }, });
内容的提问来源于stack exchange,提问作者roggc
相关产品推荐
相关产品推荐

