Is there a clean way to do optional relations in t...
# orm-help
s
Is there a clean way to do optional relations in the case where sometimes the relation is linked to one table but other times it is linked to another. For example:
Copy code
model Circle {
  id         Int        @id @default(autoincrement())
  name       String
  shape      Shape?
}

model Square {
  id              Int        @id @default(autoincrement())
  name            String
  shape           Shape?
}

model Shape {
  id        Int      @id @default(autoincrement())
  type      String
  typeId    Int
  circle    Circle?   @relation(fields: [typeId], references: [id])
  square    Square?   @relation(fields: [typeId], references: [id])
}
I think what I am looking for is "optional" relation
👍 1
j
I think that would be called a polymorphic relation. And no, Prisma does not help you with that out of the box yet as databases usually do not directly support that. Here are two relevant issues: https://github.com/prisma/prisma/issues/1644 https://github.com/prisma/prisma/issues/2505
Instead you would do 2 relations, and then set one or another depending on which one is "active".
s
Thanks!
👍 1