Skip to content

Prisma ORM v8 removes required empty fields from field-less Mongo variant during contract emit #30130

Description

@Shgit29

Package and version

@internal/mongo-contract-psl — prisma/prisma main @ 10c57bc

What happened?

This is a re-file of prisma/prisma-next#1070 after Prisma Next development moved to prisma/prisma.

The issue still reproduces on the latest main.

For a Mongo polymorphic variant with no fields of its own, the PSL interpreter correctly produces fields: {}. Generic contract canonicalization then removes that empty object.

The resulting contract is invalid because fields is required by the Mongo contract schema.

Observed result:

BEFORE_NOTE={"fields":{},"relations":{},"storage":{"collection":"posts"},"base":{"namespace":"__unbound__","model":"Post"}}
BEFORE_HAS_FIELDS=true

AFTER_NOTE={"base":{"model":"Post","namespace":"__unbound__"},"relations":{},"storage":{"collection":"posts"}}
AFTER_HAS_FIELDS=false

CONTRACT_VALID=false
VALIDATION_ERROR=domain.namespaces.__unbound__.models.Note.fields must be an object (was missing)

What did you expect to happen?

Canonicalization should preserve the required empty fields object for a field-less variant.

The canonicalized contract should therefore retain:

{
  "fields": {}
}

and pass MongoContractSchema validation.

Minimal reproduction

Using the current Mongo PSL interpreter, canonicalizer, and contract schema with:

model Post {
  id    ObjectId @id @map("_id")
  title String
  kind  String

  @@discriminator(kind)
  @@map("posts")
}

model Note {
  @@base(Post, "note")
}
  1. Interpret the schema through the Mongo PSL interpreter.
  2. Verify that Note.fields is {}.
  3. Canonicalize the resulting contract.
  4. Inspect Note.fields again.
  5. Validate the canonicalized contract with MongoContractSchema.

Current result:

Before canonicalization: Note.fields exists and equals {}
After canonicalization: Note.fields is missing
MongoContractSchema validation: fails

Environment

  • Prisma repository: prisma/prisma
  • Commit: 10c57bcca34f1bd10ddd01571f82fa9ce4280841
  • Branch: main
  • Node: 24.19.0
  • OS: Fedora Linux
  • Package manager: pnpm
  • Target database family: MongoDB

Additional context

Original report: prisma/prisma-next#1070

The issue was closed because the prisma/prisma-next repository is being archived, not because the bug was fixed.

I re-tested this against the latest prisma/prisma main, where it still reproduces.

The current canonicalizer has preservation handling for required empty model relations and storage objects, but no equivalent handling for fields.

A PR with a fix and regression coverage is already open: #30003

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