updateMany with relationMode = "prisma" updates the cross product of a compound primary key

Open
#31,264 1 comment 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
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-pg 7.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

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.