Skip to content

[Feature Request] Support Custom Mapping for @@delegate Type Attribute #2558

@motopods

Description

@motopods

Feature Request: Support Custom Mapping for @@delegate Type Attribute

Background

In the current implementation of polymorphism in ZenStack (@@delegate), the discriminator field (type) automatically uses the model (table) name as its value.

This creates limitations when developers want to:

  • Use an enum as the discriminator
  • Define custom type values that differ from model names

Problem

When using an enum for the discriminator field, the enum values must exactly match the model names, otherwise it results in a runtime or schema validation error.

Example:

enum ContentType {
  PostContent
  VideoContent
}

model Content {
  id   Int          @id
  type ContentType

  @@delegate(type)
}

model PostContent extends Content {}
model VideoContent extends Content {}

This works only because enum values match model names (PostContent, VideoContent).

However, if we want:

enum ContentType {
  post
  video
}

It will fail because post !== PostContent.

Proposal

Introduce a way to customize the mapping between model names and discriminator values, for example via a map-like attribute on @@delegate or the child models.

Option 1: Map on child models
model PostContent extends Content {
  @@delegate.map("post")
}

model VideoContent extends Content {
  @@delegate.map("video")
}
Option 2: Map on parent models @@delegate
model Content {
  id   Int          @id
  type ContentType

  @@delegate(type, map: {
    PostContent: "post",
    VideoContent: "video"
  })
}
Option 3: Map defined in child models @@delegateMap
enum ContentType {
  Post
  Video
}

model PostContent extends Content {
  @@delegateMap("post") // for String
  @@delegateMap(Post) // for Enum
}

model VideoContent extends Content {
  @@delegateMap("video") // for String
  @@delegateMap(Video)   // for Enum
}

Benefits

  • Allows flexible discriminator values independent of model names
  • Enables cleaner integration with existing schemas or APIs
  • Makes enum usage more ergonomic and less error-prone
  • Avoids forcing naming conventions solely due to framework constraints

Additional Notes

  • Default behavior can remain unchanged (fallback to model name)
  • Validation should ensure mapping completeness and uniqueness
  • This feature aligns with Prisma’s @map / @@map philosophy

Summary

Currently, @@delegate tightly couples discriminator values to model names, which limits flexibility. Adding support for custom mapping would significantly improve developer experience and schema design flexibility.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions