如何解决您能帮我弄清楚如何在 Prisma 和 Nexus 中连接两个多对多模型帖子和标签吗?
我想创建一个附有标签列表的帖子。模型是多对多连接的(一个帖子可以有多个标签,一个标签可以有多个帖子)。
这是我的棱镜模型:
model Post {
id String @id @default(cuid())
slug String @unique
title String
body String
tags Tag[]
}
model Tag {
id String @id @default(cuid())
posts Post[]
name String
slug String @unique
}
这是我尝试创建帖子并为其附加标签的变化:
t.field('createPost',{
type: 'Post',args: {
title: nonNull(stringArg()),body: stringArg(),tags: list(arg({ type: 'TagInput' }))
},resolve: async (_,args,context: Context) => {
// Create tags if they don't exist
const tags = await Promise.all(
args.tags.map((tag) =>
context.prisma.tag.upsert({
create: omit(tag,"id"),update: tag,where: { id: tag.id || "" },})
)
)
return context.prisma.post.create({
data: {
title: args.title,body: args.body,slug: `${slugify(args.title)}-${cuid()}`,tags: {
//disconnect: [How do I disconnect already existing tags???]
connect: [{id:"ckql6n0i40000of9yzi6d8bv5"}]
},authorId: getUserId(context),published: true,// make it false once Edit post works.
},})
},})
这似乎不起作用。
Invalid `prisma.post.create()` invocation:
{
data: {
title: 'Post with tags',body: 'Post with tags body',slug: 'Post-with-tags-ckql7jy850003uz9y8xri51zf',tags: {
connect: [
{
id: 'ckql6n0i40000of9yzi6d8bv5'
}
]
},}
}
UnkNown arg `tags` in data.tags for type PostUncheckedCreateInput. Available args:
type PostUncheckedCreateInput {
id?: String
title: String
body: String
slug: String
}
帖子上的 tags
字段似乎丢失了?但我确实运行了 prisma generate
和 prisma migrate
,还有什么可能导致此问题?
我的第二个问题是 - 如何用新标签替换已连接的标签?例如,在 updatePost
突变中,某些标签可能在更新之前已经存在,但随后用户想将它们从帖子中删除。如何用新列表正确替换所有标签?如果我应该先 disconnect
所有旧标签,然后 connect
新标签 - 有没有办法在同一个突变中做到这一切?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。