Skip to content
HA_

← All writing

· 2 min read

One raw query, and Prisma rounded my numbers

A batch transaction that started with a raw query quietly changed how every later query in it was compiled. Json numbers that need full precision came back one step off. The core of the fix, merged in Prisma, is one condition.

open-sourceprismadatabases

Silent data changes are the worst kind of bug: nothing throws, the write succeeds, and the stored value is not the value you sent. This is one I fixed in Prisma (prisma/orm#31025, merged 19 November 2026).

The symptom

In a batch transaction like this one:

await prisma.$transaction([
  prisma.$executeRaw`select 1`,
  prisma.probe.create({ data: { data: { a: 25.900000000000002 } } }),
])

the Json value was stored as 25.9. Doubles that need all 17 significant digits came back one step away from what was written. Remove the raw query and the same create stored the exact value.

The cause

Before compiling a batch, Prisma parameterizes its queries: values become placeholders and reach the database exactly as given. That step was skipped when the first query in the batch was raw, a shortcut meant for batches made only of raw queries.

In a mixed batch, the shortcut also skipped every model query that came after the raw one. Their values were compiled into the query plan as literals by the query compiler, and doubles that need all 17 digits lost the last one on the way.

It was worse than rounding. Query plans are cached by the batch's shape, so a plan with the first batch's values baked in could be reused: my regression test showed a second batch of the same shape storing the first batch's values instead of its own.

The fix

Parameterize the batch unless every query in it is raw. Raw queries pass through parameterization unchanged, as they already did when a model query came first. The core change is one condition. A maintainer, while finishing the PR, also stopped caching batches that contain a raw query, since raw values are not parameterized and would end up in the cache key.

Most of my work was the regression test on PostgreSQL: a raw query followed by creates with five doubles that need 17 digits, read back exactly, and the same batch shape run again with new values to prove those are stored. Both fail before the fix and pass after.

What I took from it

When a fast path depends on the shape of the input, test the mixed case. "All raw" and "all model" were both covered. The batch that mixed them was not.