Prisma currently does not support union types for fields directly in its schema. For instance, this is not possible:
model Movie {
id String @id @default(uuid())
title String
customers (Client | User)[] // <-- Not supported in Prisma
}
However, there are workarounds to achieve similar functionality:
If you need a single field to hold polymorphic data, you can store it in a Json
column. For example:
model Movie {
id String @id @default(uuid())
title String
customers Json?
}
In this case, you can store discriminated unions using libraries like Zod, but note that:
- Prisma treats it as untyped JSON.
- You lose relational features like foreign keys and constraints.
You can create a single table for "customers" with a type
discriminator field:
model Movie {
id String @id @default(uuid())
title String
customers PolymorphicCustomer[]
}
model PolymorphicCustomer {
id String @id @default(uuid())
type String // e.g., "Client" or "User"
movieId String
movie Movie @relation(fields: [movieId], references: [id])
// Additional fields for the specific types
}
Each row in PolymorphicCustomer
behaves as either a "Client" or a "User," depending on the type
value.
Create separate models for Client
and User
, and use a bridging table for the union:
model Movie {
id String @id @default(uuid())
title String
movieClients MovieClient[]
movieUsers MovieUser[]
}
// Bridging table for Clients
model MovieClient {
movieId String
clientId String
movie Movie @relation(fields: [movieId], references: [id])
client Client @relation(fields: [clientId], references: [id])
@@id([movieId, clientId])
}
// Bridging table for Users
model MovieUser {
movieId String
userId String
movie Movie @relation(fields: [movieId], references: [id])
user User @relation(fields: [userId], references: [id])
@@id([movieId, userId])
}
model Client {
id String @id @default(uuid())
name String
movies MovieClient[]
}
model User {
id String @id @default(uuid())
name String
movies MovieUser[]
}
With this approach:
- Use separate queries to fetch
movieClients
andmovieUsers
. - Merge the results in application logic for a unified "customers" list.
Prisma does not directly support fields with union types (e.g., (Client | User)[]
). To handle polymorphic data:
- Use a
Json
field for flexibility, or - Use relational models with:
- A single polymorphic table with a discriminator field.
- Separate models and bridging tables for relational integrity.
You can then use libraries like Zod or application logic to enforce and handle discriminated unions at runtime.