#Quick question on relations

3 messages · Page 1 of 1 (latest)

drifting karma
#

My schema is gonna consist of a User type, the user could either be an employee, a teacher, or a principal
Each one of them is gonna have different fields, but all of them are indeed users
how would a prisma schema look like for this ?

#

Here's ChatGPT's response, is this the norm or is this outdated?

model User {
  id       Int      @id @default(autoincrement())
  username String
  email    String   @unique
  role     String   // You can use an Enum type here if roles are predefined
  // Add other fields specific to each role
  employeeProfile EmployeeProfile? @relation(fields: [id], references: [userId])
  teacherProfile TeacherProfile?   @relation(fields: [id], references: [userId])
  principalProfile PrincipalProfile? @relation(fields: [id], references: [userId])
}

model EmployeeProfile {
  id     Int      @id @default(autoincrement())
  userId Int
  // Add fields specific to employees
  // ...
  user   User     @relation(fields: [userId], references: [id])
}

model TeacherProfile {
  id     Int      @id @default(autoincrement())
  userId Int
  // Add fields specific to teachers
  // ...
  user   User     @relation(fields: [userId], references: [id])
}

model PrincipalProfile {
  id     Int      @id @default(autoincrement())
  userId Int
  // Add fields specific to principals
  // ...
  user   User     @relation(fields: [userId], references: [id])
}
drifting karma
#

I think I'm gonna go with the Table-Per-Type solution