```model User { id Int @id @default...
# orm-help
a
Copy code
model User {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  email     String    @unique
  username  String    @db.VarChar(50)
  password  String
  ...
}

model Message {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  subject   String
  body      String
}
I'm having trouble understanding how to best setup the relationships for a 1 on 1 private messaging system. I'd like for it to work similar to how Reddit handles private messages. Could someone point me in the right direction please.
@Ryan Sorry to bug you again, would you be able to point me in the right direction with this one? I'm having trouble thinking about this conceptually. I'm not the best when it comes to setting up these data models.
🤔 If you know of any courses that teach this stuff (database design), I'd also appreciate a link to those.
r
@Awey 👋 Sure, what you could do is create a messaging room and have 2 users present in the room as this is just a 1-1 chat. That would be connected to the messages model. Let me share the schema with you.
Copy code
model User {
  id       Int       @id @default(autoincrement())
  name     String
  rooms    Room[]
  messages Message[]
}

model Room {
  id       Int       @id @default(autoincrement())
  messages Message[]
  users    User[]
}

model Message {
  id      Int    @id @default(autoincrement())
  content String
  user    User   @relation(fields: [userId], references: [id])
  Room    Room?  @relation(fields: [roomId], references: [id])
  userId  Int
  roomId  Int?
}
This is something that you could use as a starter.
a
Is this model architecutre for live chat or how I would structure it for an email like system between userss?
r
Yes you can use this for a live chat as well as a simple messaging system. An basic email system would also work but an advanced one with something like threads and reply-all would need more design.