Prisma 架构和评论查询以及评论,这是对单个表中评论的重播

问题描述 投票:0回答:1

我有一个带有用户评论的基本聊天系统,我现在添加了一个回复方法,但无法创建我认为可能的自我关系。

    model shoutMessage {
      id        String          @id @default(cuid())
      createdAt DateTime        @default(now())
      message   String
      authorId  String
      type      Int
      parent    String?
      like      Int             @default(0)
      author    User            @relation(fields: [authorId], references: [id], onDelete: Cascade)
      likes     tb_pbot_shout[]
      root   shoutMessage? @relation("replied", fields: [parent], references: [id], onDelete: Cascade)
  replies     shoutMessage[] @relation("replied")
    }

基本评论,然后我添加了一个parentId来标记为当然回复。我以为我可以与自身建立一种关系,但无法让它发挥作用。然后提前思考,我认为该关系足以在单个查询中完成整个树

const shouts = prisma.shoutMessage.findMany({
  select: { id,true,
  .... replies, true,
}
});

我相信架构是正确的,当我添加重播时,我收到以下没有意义的错误,如果我添加一个没有父级的新根注释,那么它会按预期工作?

这是错误

Invalid `prisma.shoutMessage.create()` invocation:

{
  data: {
    author: {
      connect: {
        id: "cltna90na0002r4wz7dktapnp"
      }
    },
    message: "<p>asdasdasdasdasd</p>",
    parent: "cltw3r6uy0001md3c0lrnfbla",
    ~~~~~~
    type: 1,
?   id?: String,
?   createdAt?: DateTime,
?   like?: Int,
?   likes?: tb_pbot_shoutCreateNestedManyWithoutShoutInput,
?   root?: shoutMessageCreateNestedOneWithoutReplyInput,
?   reply?: shoutMessageCreateNestedManyWithoutRootInput
  }
}

Unknown argument `parent`. Available options are marked with ?.
    at async replyShout (./app/chat2/_lib/process.tsx:272:22)
    at async $$ACTION_2 (./app/chat2/page.tsx:173:5)
digest: "1873872561"

解决方案,必须添加为root,然后连接父级。

 let newComment = await prisma.shoutMessage.create({
  
    data: {
      author: { connect: { id: user?.id } },
      message: message,
      root: { connect: { id: messageId } },
      type: type,
    },
  });
prisma self relation
1个回答
0
投票

不能简单地添加父级,必须将回复连接到父级

  let newComment = await prisma.shoutMessage.create({
    data: {
      author: { connect: { id: user?.id } },
      message: message,
      root: { connect: { id: messageId } },
      type: type,
    },
  });
© www.soinside.com 2019 - 2024. All rights reserved.