Is it possible to create relationships between one...
# orm-help
k
Is it possible to create relationships between one model, and several other models. Something like Posts, Photos, etc. all have comments where all comments live in the comment table?
โœ… 1
r
Hi @Kelly Copley - yes, it is
I was trying to find an example but can't right now ๐Ÿ˜„ I have one somewhere but it definitely is possible..
(and no "hacks" required)
n
Hey Kelly ๐Ÿ‘‹ You could try something like this:
Copy code
model Post {
  id          Int       @id @default(autoincrement())
  title       String
  content     String
  publishedOn DateTime  @default(now())
  authorId    String
  comments    Comment[]
}

model Photo {
  id          Int       @id @default(autoincrement())
  photoUrl    String
  publishedOn DateTime  @default(now())
  authorId    String
  comments    Comment[]
}

model Comment {
  id          Int      @id @default(autoincrement())
  content     String
  publishedOn DateTime @default(now())
  authorId    String
  Post        Post?    @relation(fields: [postId], references: [id])
  Photo       Photo?   @relation(fields: [photoId], references: [id])
  photoId     Int?
  postId      Int?
}
k
@Nurul Yeah I guess this makes sense. I think my brain was sort of hoping that there was some way to not have to store 9 null fields in a table if I have 10 models that could have related comments, but to do that would definitely be a special case feature.
n
I have seen this feedback before and we are discussing this internally on how could we improve the DX in this case.
k
Sorry for the late response.. I'd love to hear any updates on this. If at any point there is an open issue I could follow for updates that would be perfect.
n
I canโ€™t find a GitHub Feedback Issue for this particular case, Could you create one here?