0

I have a User model which can post Comments and vote on them. Comments can also be parented to each other (for nested comments like on HN or 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")
}

I have a few questions on how to correctly name these relations.

How should relation that describes the User being an author of several Comments be named:

  • "UserToComment"?
  • "Author"?
  • "CommentAuthor"?
  • "UserComments"?

I've read somewhere that many-to-many relation names are supposed to start with an underscore (but I'm not sure why, and whether this applies to one-to-many relations as well). How should the relation that describes the multiples Users being upvoters of multiple Comments be named:

  • "_UpvoterToComment"?
  • "UpvoterToComment"?
  • "UserUpvoted"?
  • "Upvoters"?

Finally, I have a Comment relating to itself for the nested comments. Should this relation be named:

  • "Parent"?
  • "Children"?
  • "ParentChildren"?
  • "CommentToComment"?
  • Any of the above but with the underscore in front?

Can you guys help me to understand the correct conventions here, give me some advice on how should name my relations?

lumenwrites
  • 1,287
  • 4
  • 18
  • 35

1 Answers1

2

General Advice on naming in prisma

Naming is a bit subjective. Normally, I think you should do what makes the most sense for you and your use-case. There are some conventions and rules that Prisma recommends, that you should follow as well:

However, there aren't any guidelines on naming relations.

Naming for the specific usecases mentioned

In general, go with whatever you think concisely describes the relationship itself. I can give you my personal opinion about the specific naming questions you brought up:

How should relation that describes the User being an author of several Comments be named:

I think UserComments make sense.

I've read somewhere that many-to-many relation names are supposed to start with an underscore.

This is something that Prisma does internally when automatically generating the underlying SQL. You don't have to worry about this at all, nor should you explicitly add a _ at the beginning of a name.

How should the relation that describes the multiples Users being upvoters of multiple Comments be named:

I would go with Upvoters.

Finally, I have a Comment relating to itself for the nested comments. Should this relation be named:

I would probably go with something like CommentResponses or CommentReplies.

Tasin Ishmam
  • 5,670
  • 1
  • 23
  • 28