Skip to content

feat(client): add initial support for polymorphic relations#29306

Draft
ian wants to merge 13 commits intoprisma:mainfrom
ian:feat/1644-support-polymophic-associations
Draft

feat(client): add initial support for polymorphic relations#29306
ian wants to merge 13 commits intoprisma:mainfrom
ian:feat/1644-support-polymophic-associations

Conversation

@ian
Copy link

@ian ian commented Mar 5, 2026

This adds the foundation for polymorphic relations (e.g., item Post | Comment):

  • Add isPolymorphic, relationTypes, relationDiscriminator to DMMF types
  • Generate union types for polymorphic relation outputs in TS/JS generators
  • Add { on: 'Post' } syntax support for include/select filtering
  • Handle discriminator-based query routing in runtime
  • Add test fixture for polymorphic relations

Note: Schema parser changes in prisma-engines are still needed to parse the Post | Comment syntax and @polymorphic attribute.

Example Usage

###Schema

model Vote {
  id        String         @id @default(cuid())
  item      Post | Comment @polymorphic(discriminator: itemType)
  itemId    String
  itemType  String
}

model Post {
  id    String @id @default(cuid())
  title String
  votes Vote[]
}

model Comment {
  id   String @id @default(cuid())
  body String
  votes Vote[]
}

Querying

// Union type (Post | Comment)
const vote = await prisma.vote.findUnique({
  where: { id: '1' },
  include: { item: true },
})
// vote.item: Post | Comment

// Filtered by `on` (Post only)
const voteWithPost = await prisma.vote.findUnique({
  where: { id: '1' },
  include: { item: { on: 'Post' } },
})
// voteWithPost.item: Post | null

// Filter by discriminator
const postVotes = await prisma.vote.findMany({
  where: { itemType: 'Post' },
  include: { item: true },
})

This adds the foundation for polymorphic relations (e.g., item Post | Comment):

- Add isPolymorphic, relationTypes, relationDiscriminator to DMMF types
- Generate union types for polymorphic relation outputs in TS/JS generators
- Add { on: 'Post' } syntax support for include/select filtering
- Handle discriminator-based query routing in runtime
- Add test fixture for polymorphic relations

Note: Schema parser changes in prisma-engines are still needed to
parse the Post | Comment syntax and @polymorphic attribute.
@CLAassistant
Copy link

CLAassistant commented Mar 5, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ ian
❌ devin-ai-integration[bot]
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 5, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 09b86b81-a810-4224-9b71-7d50138f3485

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can enforce grammar and style rules using `languagetool`.

Configure the reviews.tools.languagetool setting to enable/disable rules and categories. Refer to the LanguageTool Community to learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants