Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Prisma rejects a unique query because where holds no usable unique field

Your error

Argument `where` of type UserWhereUniqueInput needs at least one of `id` arguments. Available options are marked with ?.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The unique value is undefined at runtime

Check the input before the query and return a 400 or 404 yourself.

Fix
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "bad id" });
const user = await prisma.user.findUnique({ where: { id } });

// schema.prisma
generator client {
  provider        = "prisma-client"
  output          = "./generated"
  previewFeatures = ["strictUndefinedChecks"]
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Argument where of type UserWhereUniqueInput needs at least one of id arguments means the where object of findUnique, update, delete or upsert reached Prisma with no unique field set. Usually the id is undefined, which Prisma drops from the query. Otherwise only non-unique fields were given, or a compound key was split. Applies to Prisma ORM 7.10.

  • Applies to Prisma ORM 7.10.1, Node.js 24, TypeScript 5.4 or later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the exact where object just before the failing call, then list which fields the schema marks as unique. An undefined value points to cause 1; a defined value on a field without @id or @unique points to cause 2; fields that only appear together in @@unique point to cause 3.

Run this first
console.dir({ where }, { depth: null }); // in your code, right before the query
grep -nE "@id|@unique|@@id|@@unique" prisma/schema.prisma

Then find the line you see in this list and open its fix:

  1. 1The unique value is undefined at runtimeThe printed object shows id: undefined, often from a misspelled route param, an empty session or a missing query string
  2. 2Only non-unique fields, or the unique field sits inside AND, OR or NOTThe value is defined, but the field (email, slug, status) has no @unique in the schema, or the unique field is wrapped in AND
  3. 3A compound unique key is passed as separate fieldsThe schema has @@unique([postId, userId]) or @@id, and the error lists a name such as postId_userId among the options

What does the error look like?

  • Argument `where` of type UserWhereUniqueInput needs at least one of `id` arguments. Available options are marked with ?.
  • Argument `where` of type StationWhereUniqueInput needs at least one of `id` or `name` arguments
  • Invalid value for argument `where`: explicitly `undefined` values are not allowed.

Cause 1 of 3

The unique value is undefined at runtime

You seeThe printed object shows id: undefined, often from a misspelled route param, an empty session or a missing query string

Check

UserWhereUniqueInput needs at least one of id arguments appears because Prisma leaves any key whose value is undefined out of the generated query. The where object becomes empty, and TypeScript does not flag an undefined id here, which Prisma tracks as a confirmed bug.

Check
grep -rn "findUnique\|findUniqueOrThrow" src/ | head

Fix

Check the input before the query and return a 400 or 404 yourself. To make Prisma fail loudly on stray undefined values, turn on the strictUndefinedChecks preview feature, which throws a clearer error, and use Prisma.skip where leaving a field out is intended.

Fix
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "bad id" });
const user = await prisma.user.findUnique({ where: { id } });

// schema.prisma
generator client {
  provider        = "prisma-client"
  output          = "./generated"
  previewFeatures = ["strictUndefinedChecks"]
}

Verify

A request without an id now gets your own 400 response, and a valid id returns the record.

Verify
npx prisma generate && curl -i http://localhost:3000/users/1

Confirmed by prisma.io

Did this fix it?

Cause 2 of 3

Only non-unique fields, or the unique field sits inside AND, OR or NOT

You seeThe value is defined, but the field (email, slug, status) has no @unique in the schema, or the unique field is wrapped in AND

Check

The where input of single-record operations accepts every field of the model, yet the client reference requires at least one unique field outside boolean operators. Extra non-unique fields are allowed next to it, but they cannot replace it.

Check
grep -n "email" prisma/schema.prisma

Fix

Use findFirst or updateMany when the field is not unique. If it should be unique, add @unique and migrate. When you need an extra condition, put the unique field at the top level and the other fields beside it.

Fix
// not unique: use findFirst
const user = await prisma.user.findFirst({ where: { email } });

// unique id plus a guard, no AND wrapper
await prisma.user.update({
  where: { id: user.id, version: user.version },
  data: { city: "Berlin", version: { increment: 1 } },
});

Verify

The query runs and returns one record, or null for findFirst when nothing matches.

Verify
npx tsc --noEmit

Confirmed by prisma.io

Did this fix it?

Cause 3 of 3

A compound unique key is passed as separate fields

You seeThe schema has @@unique([postId, userId]) or @@id, and the error lists a name such as postId_userId among the options

Check

Fields that are unique only together do not count one by one. Prisma exposes the pair as one generated key, named after the fields joined by an underscore or after the name argument of @@id or @@unique, and unique queries must use that key.

Check
grep -n "@@unique\|@@id" prisma/schema.prisma

Fix

Nest the values under the compound key name.

Fix
const like = await prisma.like.findUnique({
  where: {
    postId_userId: { postId: 1, userId: 1 },
  },
});

Verify

The query returns the matching row and TypeScript accepts the shape.

Verify
npx tsc --noEmit

Confirmed by prisma.io

Did this fix it?

How do I stop it coming back?

  • Validate route params and session ids with a schema library before any findUnique, update or delete call.
  • Enable strictUndefinedChecks in development so an undefined filter fails with a clear message instead of an empty where.
  • Name compound constraints with @@unique(name: ...) so the key used in queries stays readable and stable.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.