[BUG]: Weak-type excess-property checking lets invalid keys/values through `where`, `orderBy`, and `columns` once one valid key is present
Report hasn't been filed before.
- I have verified that the bug I'm about to report hasn't been filed before.
What version of drizzle-orm are you using?
1.0.0-rc.4
What version of drizzle-kit are you using?
1.0.0-rc.4
Other packages
No response
Describe the Bug
This is expansion of issue 5644.
Undesired behavior
Several relational query options — where, orderBy, and columns — stop being type-checked for invalid keys (and, for orderBy/columns, invalid values) as soon as the object also contains at least one valid key. This defeats the compile-time safety that's supposed to catch a renamed/removed column.
Steps to reproduce
import { drizzle } from 'drizzle-orm/node-postgres';
import { pgTable, text } from 'drizzle-orm/pg-core';
import { defineRelations } from 'drizzle-orm';
const user = pgTable('user', {
id: text().primaryKey(),
name: text(),
});
const relations = defineRelations({ user });
const db = drizzle({
connection: { connectionString: '...' },
relations,
});
// (1) TypeScript correctly errors — no valid keys present
db.query.user.findFirst({
where: { nameZZZZ: 'someValue' },
});
// (2) TypeScript does NOT error — `id` is valid, so `nameZZZZ` silently passes
db.query.user.findFirst({
where: { id: '123', nameZZZZ: 'someValue' },
});
// (3) Same leak, plus it extends to orderBy and columns, and there it also
// swallows an invalid VALUE type, not just an invalid key
db.query.user.findFirst({
where: { id: '123', nameZZZZ: 'someValue', nonExistentField: 'value' },
orderBy: { id: 'asc', namesZZZZ: 'INVALID VALUE' }, // wrong key AND invalid value, neither caught
columns: { id: true, namesZZZZ: 'INVALID VALUE' }, // wrong key AND invalid value, neither caught
});Desired result
Every one of where, orderBy, and columns should flag unrecognized keys the same way case (1) does, regardless of whether other valid keys are present in the same object — and orderBy/columns should also validate that each value matches its expected type ('asc' | 'desc' for orderBy, boolean for columns) rather than accepting arbitrary strings once the key-presence check is bypassed.
Impact of this problem
After renaming/removing field in schema, TypeScript does not catch the errors, everything seems perfect at compile time but things will break in production. Also, chances of mistyping things without noticing are much bigger.
Environment
- Driver:
drizzle-orm/node-postgres(pg) - Not a monorepo — single
package.jsonat project root - TypeScript version: 5.9.3 (
"typescript": "^5"in devDependencies) - Full tsconfig.json:
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"target": "ES2017",
"module": "esnext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"erasableSyntaxOnly": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
"plugins": [{ "name": "next" }],
"incremental": true,
"resolveJsonModule": true,
"isolatedModules": true,
"strict": true,
"strictNullChecks": true,
"skipLibCheck": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false,
"exactOptionalPropertyTypes": false,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noImplicitReturns": false,
"noPropertyAccessFromIndexSignature": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"esModuleInterop": true,
"paths": {
"@/*": ["./src/*"],
"@packages/*": ["./packages/*"]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
".next/dev/types/**/*.ts",
"**/*.mts"
],
"exclude": ["node_modules"]
}
Source: drizzle-team/drizzle-orm