【问题标题】:What is the correct way to name relations in Prisma? Are there guidelines or conventions I should follow?在 Prisma 中命名关系的正确方法是什么?是否有我应该遵循的指导方针或约定?
【发布时间】:2022-01-02 19:14:21
【问题描述】:

我有一个User 模型,可以发布Comments 并对它们进行投票。 Comments 也可以是彼此的父级(对于嵌套的 cmets,如 HN 或 Reddit`)。

model User {
  [...]
  // Comments the user has posted
  comments Comment[] @relation("UserToComment")
  // Comments the user has voted on
  upvotedComments Comment[] @relation("_UpvoterToComment")
  downvotedComments Comment[] @relation("_DownvoterToComment")  
}

model Comment {
  [...]
  // Comment's author
  author User @relation("UserToComment", fields: [authorId], references: [id])
  authorId String

  // Users who voted on the comment
  upvoters User[] @relation("_UpvoterToComment")
  downvoters User[] @relation("_DownvoterToComment")
  
  // One-to-many self-relation for nested comments
  parentId String?
  parent Comment? @relation("Parent", fields: [parentId], references: [id])
  children Comment[] @relation("Parent")
}

我有几个关于如何正确命名这些关系的问题。

描述User是多个Comments的作者的关系应该如何命名:

  • “UserToComment”?
  • “作者”?
  • “评论作者”?
  • “用户评论”?

我在某处读到多对多关系名称应该以下划线开头(但我不确定为什么,以及这是否也适用于一对多关系)。描述多个Users 是多个Comments 的支持者的关系应该如何命名:

  • “_UpvoterToComment”?
  • “UpvoterToComment”?
  • “用户投票”?
  • “支持者”?

最后,我有一个与嵌套 cmets 相关的 Comment。这个关系应该被命名:

  • “父母”?
  • “儿童”?
  • “父母子女”?
  • “CommentToComment”?
  • 以上任何一项但前面有下划线?

你们能帮我理解这里的正确约定吗,给我一些关于如何命名我的关系的建议?

【问题讨论】:

    标签: prisma prisma2


    【解决方案1】:

    关于在 prisma 中命名的一般建议

    命名有点主观。通常,我认为您应该做对您和您的用例最有意义的事情。 Prisma 推荐了一些约定和规则,您也应该遵守:

    但是,没有任何关于命名关系的准则

    为提到的特定用例命名

    一般而言,请使用您认为简洁描述关系本身的任何内容。对于您提出的具体命名问题,我可以个人意见

    描述用户是多个评论作者的关系应该如何命名:

    我认为UserComments 有道理。

    我在某处读到多对多关系名称应该以下划线开头。

    这是 Prisma 在自动生成底层 SQL 时在内部执行的操作。您完全不必担心这一点,也不应在名称的开头显式添加_

    描述多个用户是多个评论的赞成者的关系应该如何命名:

    我会选择Upvoters

    最后,我有一个关于嵌套 cmets 的评论。这个关系应该被命名:

    我可能会选择CommentResponsesCommentReplies

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 2010-10-08
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多