updateMany with relationMode = "prisma" updates the cross product of a compound primary key
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 58/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- postgresql, typescript
Research direction
Start by reading update_many_records and the related work in prisma/prisma-engines#5868; compare the UPDATE filter with the pre-read key handling and the deleteMany behavior described here. Reproduce with the supplied compound-key schema and PostgreSQL rows, then check that only the two matching tuples are updated and count is 2, including the relation onUpdate: Cascade or SetNull case.
Written by the indexing model from the issue text.
Description
Package and version
prisma 7.9.0, @prisma/client 7.9.0, @prisma/adapter-pg 7.9.0
What happened?
With relationMode = "prisma", updateMany on a model with a compound primary key updates rows that its where does not match.
The query compiler pre-reads the matching rows' key columns, then runs the UPDATE with one IN list per key column and without the original filter (AND 1=1). The IN lists match every combination of the pre-read values, so any row whose key values each appear somewhere in the pre-read is updated, not only the pre-read rows themselves.
Two rows match the where below. All four rows are updated, and count is 4:
SELECT "public"."Stage"."taskId", "public"."Stage"."id" FROM "public"."Stage"
WHERE (("public"."Stage"."taskId" = $1 AND "public"."Stage"."id" = $2) OR ("public"."Stage"."taskId" = $3 AND "public"."Stage"."id" = $4)) OFFSET $5
-- ["1","10","2","20","0"]
UPDATE "public"."Stage" SET "status" = $1
WHERE ("public"."Stage"."taskId" IN ($2,$3) AND "public"."Stage"."id" IN ($4,$5) AND 1=1)
-- ["done","1","2","10","20"]
COMMIT
This needs no concurrency: the wrong rows are written on every call whenever the matched rows have two or more distinct values in each key column. deleteMany on the same where deletes only the 2 matching rows, because its DELETE keeps the original filter next to the same IN lists.
What did you expect to happen?
Only (1, 10) and (2, 20) are updated, and count is 2. The pre-read keys should be matched as tuples, e.g. ("taskId", "id") IN (($2,$3), ($4,$5)), or the original filter should stay on the UPDATE, as it does for deleteMany.
Minimal reproduction
generator client {
provider = "prisma-client"
output = "../generated"
moduleFormat = "cjs"
}
datasource db {
provider = "postgresql"
relationMode = "prisma"
}
model Stage {
taskId BigInt
id BigInt
status String
@@id([taskId, id])
}
CREATE TABLE "Stage" ("taskId" bigint, id bigint, status text NOT NULL, PRIMARY KEY ("taskId", id));
INSERT INTO "Stage" VALUES (1, 10, 'new'), (1, 20, 'new'), (2, 10, 'new'), (2, 20, 'new');
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from './generated/client'
const prisma = new PrismaClient({
adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL }),
})
const res = await prisma.stage.updateMany({
where: { OR: [{ taskId: 1n, id: 10n }, { taskId: 2n, id: 20n }] },
data: { status: 'done' },
})
console.log(res) // { count: 4 }
console.log(await prisma.stage.findMany({ orderBy: [{ taskId: 'asc' }, { id: 'asc' }] }))
// (1,10), (1,20), (2,10) and (2,20) all have status 'done'
The model has no relations; relationMode = "prisma" alone triggers the pre-read.
Environment
- Node.js 26.13.1
- PostgreSQL 18.7
- macOS (arm64)
- Driver adapter:
@prisma/adapter-pg7.9.0
Additional context
Probably the same root cause as #30758: update_many_records runs the UPDATE with Filter::empty() when relations are emulated. That issue describes a single-column id, where the dropped filter shows up as a lost update under concurrency. With a compound key the same plan writes rows outside the filter on every call.
prisma/prisma-engines#5868 restores the filter on the UPDATE, which would prevent this, but it keeps the current plan for updates that change a field used by a relation with onUpdate: Cascade or SetNull. On a compound-key model those would still update the cross product. Matching the pre-read keys as tuples would close that case as well.
I have only tested 7.9.0. The 7.10.0 and 7.10.1 release notes don't mention a change here.
- 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