MongoDB: Base-collection creates skip encoding variant-only ObjectId fields

Open
#30,781 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #30786 by @lazerg — closed without merging

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
mongodb, typescript
Domain
backend, databases

Research direction

Start by comparing base collection creation with the explicit .variant('expense') path, using the variant-only categoryId field as the focus. The report provides an equivalent Asset/Photo fixture and three passing runtime checks; use that fixture to reproduce the validator failure and confirm whether the base path encodes the selected variant's ObjectId field. Done means base creation stores the hex string as a BSON ObjectId. An open linked pull request (#30786) already addresses this issue.

Written by the indexing model from the issue text.

Description

Package and version

@prisma/[email protected], [email protected]

These are the versions from the original report. The failure was also reproduced with the local @internal/[email protected] source at checkout e18525ac28, which had pre-existing local changes. This was not a test of a freshly installed published release candidate.

What happened?

Creating a record through a polymorphic base collection skips encoding ObjectId fields declared only on the selected variant—even when the payload includes the discriminator value.

For example, an expense created through db.orm.transactions.create(...) sends its variant-only categoryId string unchanged to MongoDB. The collection validator rejects it with MongoServerError: Document failed validation, reporting that categoryId has BSON type string instead of objectId.

This prevents reusing ID strings returned by the ORM without explicitly selecting a variant or manually converting values to ObjectId instances.

What did you expect to happen?

The create operation uses the payload's discriminator to resolve the variant's fields and encode valid ObjectId hex strings as BSON ObjectIds, consistently with an explicitly selected .variant('expense') create.

Minimal reproduction

The following is a reduced example based on the original report. Runtime verification used the repository's equivalent Asset/Photo contract fixture, where ownerId is an ObjectId field declared only on Photo. The test installed a MongoDB validator requiring that field to be a BSON ObjectId; contract emission and CLI initialization were not rerun. The exact Transaction/Expense snippet below has not been run independently.

Define this MongoDB contract, with both models in the same schema file:

// use prisma-8
model Transaction {
  id   ObjectId @id @map("_id")
  kind String
  @@discriminator(kind)
  @@map("transactions")
}
model Expense {
  categoryId ObjectId
  @@base(Transaction, "expense")
}

Emit the contract and initialize the MongoDB collection validator. No existing dataset is needed. Then create through the base collection:

const data = {
  kind: 'expense',
  categoryId: '507f1f77bcf86cd799439011',
};
await db.orm.transactions.create(data);

The insert fails because categoryId reaches MongoDB as a string.

Workaround: explicitly select the discriminator value:

await db.orm.transactions.variant('expense').create({
  categoryId: '507f1f77bcf86cd799439011',
});

All three checks passed against the equivalent fixture:

  • Base collection + valid ObjectId hex string: rejected with Document failed validation; no document inserted.
  • Explicit variant + the same string: inserted successfully, with the stored field verified as a BSON ObjectId.
  • Base collection + manual new ObjectId(...) conversion: inserted successfully, with the stored field verified as a BSON ObjectId.
Environment

Original report environment:

  • Node.js: 24.9.0
  • OS: macOS, Darwin 24.6.0
  • Package manager: npm 11.6.0
  • Database: MongoDB 8.2.0-rc4, local single-node replica set (rs0)
  • Scaffolding: [email protected]

Runtime verification environment:

  • Node.js: 26.4.0
  • Package manager: pnpm 10.27.0
  • Database: MongoDB 8.2.6, disposable single-node replica set via mongodb-memory-server
  • Test runner: Vitest 5.0.0-rc.2; three focused integration checks passed.
Dominant language
TypeScript
Stars
47.8k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
227

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prisma/orm

All issues in prisma/orm

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.