bug: CONTRACT.VALIDATION_FAILED for chained relations and PSL_DISCRIMINATOR_AND_BASE on chained polymorphism

Open
#30,708 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
postgresql, typescript
Domain
backend, databases

Research direction

Two separate bugs share this issue. Start by reproducing both with the schemas in the body: the CONTRACT.VALIDATION_FAILED error from prisma contract emit for a nullable 1:1 relation whose FK columns are NOT NULL, and the PSL_DISCRIMINATOR_AND_BASE error from chained @@base. Find where each error code is raised and check whether the chained shape is rejected for a reason that should not apply. Done means both schemas emit or validate and the existing validation tests still pass. The issue is linked to #30446, so read that first.

Written by the indexing model from the issue text.

Description

bug/1-unconfirmed kind/bug
Package and version

prisma 8.0.0-rc.22, @prisma/orm-postgres 8.0.0-rc.17

What happened?

I'm unable to create chained relations like this one

  model StoredObject {
    id        Uuid        @id
    key       String
    filename  String
    size      Int
    createdAt Timestamptz
    mimeType  String      @default("application/octet-stream")

    asset     Asset?
  }

  model Asset {
    id                 Uuid         @id

    libName            String
    assetVariant       String

    storedObjectParent StoredObject @relation(fields: [id], references: [id], onDelete: Cascade)

    image AssetImage?
  }
  
  model AssetImage {
    id               Uuid   @id

    probeResult      Json
    asset            Asset  @relation(fields: [id], references: [id], onDelete: Cascade)
  }

This results in:

❯ nub x prisma contract emit --verbose
nub: ignoring NODE_ENV set in .env (dotenv, Next, and Vite do the same); set it in the environment instead
▸ Resolving contract source...
✔ Resolving contract source...
▸ Emitting contract...
✘ Emitting contract...
✘ [CONTRACT.VALIDATION_FAILED] Relation "image" on model "public:Asset" is nullable but every local FK column is NOT NULL (columns "id")
  docs: https://docs.prisma.io/docs/orm/v8/reference/error-reference/CONTRACT.VALIDATION_FAILED

However, when you drop AssetImage like this

  model StoredObject {
    id        Uuid        @id
    key       String
    filename  String
    size      Int
    createdAt Timestamptz
    mimeType  String      @default("application/octet-stream")

    asset     Asset?
  }

  model Asset {
    id                 Uuid         @id

    libName            String
    assetVariant       String

    storedObjectParent StoredObject @relation(fields: [id], references: [id], onDelete: Cascade)

    image AssetImage?
  }

prisma can emit contract

There is similar thing with polymorphism (@@base, @@discriminator),
when you try to make parent from a child to another model prisma throws PSL_DISCRIMINATOR_AND_BASE

  model StoredObject {
    id            Uuid   @id
    key           String
    objectVariant String
    @@discriminator(objectVariant)
  }

  model Asset {
    libName      String
    assetVariant String
    @@base(StoredObject, "asset")
    @@discriminator(assetVariant)
  }

  model AssetImage {
    format String
    width  Int
    @@base(Asset, "image")
  }

First issue has workaround where you change relation 1:1? to 1:N, prisma has no issue this. Further more, first relation in chain can be 1:1? or 1:1, only further ones have to be 1:N. Kimi K3 suggested duplicating field to id @id into id @id assetId @unique but still errors to CONTRACT.VALIDATION_FAILED.

For the second issue i haven't found any reliable workaround.

What did you expect to happen?

I've excepted to create extend base model with other in relations.
Stored object extends to assets, which extends to image/vid/text. Later on things are adderssed by asset in app.

Minimal reproduction

given in description

Environment
  • Node: 26.11.1
  • OS: Fedora Linux 44
  • PM: nub 0.9.6, bun 1.4.2
  • DB: Postgres 18.6
Additional context

Looks like related to #30446.

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.