Hey, I was wondering if there was any way to have ...
# random
j
Hey, I was wondering if there was any way to have a field called profile in the review model that can be either a Teacher or a Student so I don’t need different review models for each of them. Let’s assume that student and teacher are so different to each other that integrate both together it would be a bit confusing, but I know it’s an option
Copy code
model Student {
    id           Int          @id @default(autoincrement()
    createdAt    DateTime     @default(now())
    updatedAt    DateTime     @updatedAt
    reviews      Review[]
}

model Teacher {
    id           Int          @id @default(autoincrement()
    createdAt    DateTime     @default(now())
    updatedAt    DateTime     @updatedAt
    reviews      Review[]
}

model Review {
    id           Int          @id @default(autoincrement()
    createdAt    DateTime     @default(now())
    updatedAt    DateTime     @updatedAt
    text         String
    profile      Student or Teacher?  @relation(fields: [profileId], references: [id])
    profileId    Int
}
r
heh its not possible 1. you set id of teacher ids and student ids as autoincement integer, so there are colud be collisions, and there is no clean way to diffirentate which table are means 2. another thing that there are no way generate SQL for that as possible solution is make 4th table
Copy code
Profie {
id           Int          @id @default(autoincrement()
teacherId    Int? 
studientId    Int?
}
or one classical way to extend review table with all posible joins
Copy code
Review  {
...
teacherId Int?
studentId Int?
}
d
Hi @Juanma Perez! I'm the maintainer of Prisma Util and this feature is built-in. It allows model inheritance, so you can create an abstract model that your Student and Teacher can extend in order to be added to the relation. Let me know if you need more help, everything is documented in the README.
r
@David Hancu but i didnt see a related sloution for this case, could you please provide an example ?
j
Hey @R2D2 thanks for your message, it’s what I was doing already. I guess it’s not a bad option. Thanks very much 🙂