MongoDB: Base-collection creates skip encoding variant-only ObjectId fields
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
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 viamongodb-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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from prisma/orm
-
bug/1-unconfirmed kind/bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
get-platform: distros with ID_LIKE=alpine are not detected as musl, so glibc engines are used on Alpine-based imagesPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
prisma/orm#31281 · 1 comment ·
Maintainers usually reply within 1 day
-
adapter-pg: connectToShadowDb connects to the main database when built from a connection stringMay be free again @cestercian claimed this 10 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
prisma/orm#31196 · 1 comment ·
Maintainers usually reply within 1 day
-
[Prisma 8] Type-check time grows exponentially with chained .include() callsPossibly taken @SiKreuz claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
prisma/orm#31161 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
prisma/orm#31125 · 4 comments · 6 reactions ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
tenstorrent/ttnn-visualizer#2526 ·
Maintainers usually reply within 1 day
-
trySessionCache calls /get-session twice when minting session_data (cache miss or expired cookie)Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
neondatabase/neon-js#301 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
tashfeenahmed/freellmapi#2305 ·
Maintainers usually reply within 1 day