bug: CONTRACT.VALIDATION_FAILED for chained relations and PSL_DISCRIMINATOR_AND_BASE on chained polymorphism
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
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
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
- 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