Hi guys :wave: I'm having some trouble creating an...
# orm-help
d
Hi guys šŸ‘‹ I'm having some trouble creating an invites model. The idea is a user can have many invites, and each invite can invite 1 person only. So every user would have an inviter field (for the person who invited them) and a "invites" field with all the invites they have.
āœ… 1
m
Hi @Dog! So you are planning or thinking into put all the fields in the "user" model ?
its a relation one-to-many for the invites... and one-to-one for the inviter, right?
d
I was trying to make a separate Invite model
and yeah, thats exactly right
m
šŸ‘
d
But, how would I do the schema? Tried and couldnt
n
Hey @Dog šŸ‘‹ I’m not sure I completely followed your example. If you only need a 1-n relation (from user to user), this section in the docs might be helpful: https://www.prisma.io/docs/concepts/components/prisma-schema/relations/self-relations#one-to-many-self-relations I also tried to model your schema based on your description using a dedicated
Invite
model and came out with this:
Copy code
model User {
  id            Int       @id @default(autoincrement())
  name          String?
  hostedInvites Invite[]  @relation(name: "inviter")
  invited       Invited[] @relation(name: "invitee")
}

model Invite {
  id        Int   @id @default(autoincrement())
  inviter   User  @relation("inviter", fields: [inviterId], references: [id])
  inviterId Int
  invitee   User  @relation("invitee", fields: [inviteeId], references: [id])
  inviteeId User
}
(Note that each relation always must have a relation field on both models, this docs page about disambiguating relations should also be helpful in your scenario: https://www.prisma.io/docs/concepts/components/prisma-schema/relations#disambiguating-relations)