Ian
07/13/2021, 3:41 PMenum PostType {
default
photo
video
}
// --------------------------------------------------------------
// THIS IS STRUCTURED VIA MULTI TABLE INHERITANCE
// POST as SUPERTYPE which means table of common fields
// PHOTO and VIDEO as SUBTYPE which means table for specific fields
//
// PROS -> Easy to add subtype
//. -> No need for polymorphic association (Just put the relation on the base class)
//. -> Can atleast achieve "IMPOSSIBLE STATE SHOULD BE IMPOSSIBLE" (Depends)
// -> Clear structure (No bloated table for unrelated fields)
// CONS -> JOINS???
//. -> WHAT ELSE???
// --------------------------------------------------------------
// SUPERTYPE
model Post {
user User @relation(fields:[userId], reference: [id])
userId String
id String @id @default(cuid())
text String
type PostType
}
// SUBTYPE
model Photo {
post Post @relation(fields:[id], reference: [id])
id String @id @default(cuid())
url String
width Int
height Int
}
// SUBTYPE
model Video {
post Post @relation(fields:[id], reference: [id])
id String @id @default(cuid())
url String
aspectRatio Float
}
// ---------------------------------------
// SINGLE TABLE INHERITANCE
// PROS -> Single Table (No joins needed)
//. -> WHAT ELSE???
// CONS -> Nullable fields for uncommon column
// -> Adding subtype requires altering the whole table
//. -> Bloated
// ---------------------------------------
model Post {
user User @relation(fields:[userId], reference: [id])
userId String
id String @id @default(cuid())
text String
type PostType
url String
width Int? // Nullable here
height Int? // Nullable here
aspectRatio Float? // Nullable here
}
WHAT WOULD BE YOUR GO TO CHOICE??
A. Multi-Table Inheritance (MTI)
B. Single-Table Inheritance (STI)
Ian
07/13/2021, 3:49 PMIan
07/13/2021, 3:50 PM