drizzle-team/drizzle-orm · error · Error
Cannot pass undefined values to any set operator
Error message
Cannot pass undefined values to any set operator
What it means
Thrown by PgDialect.buildSetOperations when the first element of the setOperators array destructured at dialect.ts:451 is undefined. The dialect can only compose UNION/INTERSECT/EXCEPT from concrete operator descriptors; an undefined entry indicates the query builder state was corrupted or an internal API was misused. It is primarily a defensive guard against malformed internal state rather than a user-facing API contract.
Source
Thrown at drizzle-orm/src/pg-core/dialect.ts:454
clauseSql.append(sql` skip locked`);
}
lockingClauseSql.append(clauseSql);
}
const finalQuery =
sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClauseSql}`;
if (setOperators.length > 0) {
return this.buildSetOperations(finalQuery, setOperators);
}
return finalQuery;
}
buildSetOperations(leftSelect: SQL, setOperators: PgSelectConfig['setOperators']): SQL {
const [setOperator, ...rest] = setOperators;
if (!setOperator) {
throw new Error('Cannot pass undefined values to any set operator');
}
if (rest.length === 0) {
return this.buildSetOperationQuery({ leftSelect, setOperator });
}
// Some recursive magic here
return this.buildSetOperations(
this.buildSetOperationQuery({ leftSelect, setOperator }),
rest,
);
}
buildSetOperationQuery({
leftSelect,
setOperator: { type, isAll, rightSelect, limit, orderBy, offset },
}: { leftSelect: SQL; setOperator: PgSelectConfig['setOperators'][number] }): SQL {
const leftChunk = sql`(${leftSelect.getSQL()}) `;View on GitHub (pinned to b7862528fd)
Solutions
- Inspect the array passed to the set-operator chain; ensure every element is a concrete select and never undefined.
- If building operators dynamically, filter out falsy entries before calling union/unionAll/intersect/except: selects.filter(Boolean).
- Avoid calling set-operator helpers with sparse arrays or conditional undefined operands; guard the call site.
- If you hit this without custom code, check for a drizzle-orm version mismatch where the query builder state is partially initialized.
Example fix
// before const ops = [a, cond ? b : undefined]; db.select().from(t).union(ops[0]).union(ops[1]); // hole -> undefined // after const ops = [a, cond ? b : b2].filter(Boolean); db.select().from(t).union(ops[0]).union(ops[1]);
Defensive patterns
Strategy: validation
Validate before calling
// Before chaining set operators, ensure every operand is a defined select.
const branches = [leftSelect, ...rightSelects].filter(
(s): s is AnyPgSelect => s != null && typeof s.getSelectedFields === 'function',
);
if (branches.length < 2) {
throw new Error('Set operator requires at least two valid selects');
}
const result = union(branches[0], branches[1], ...branches.slice(2)); Type guard
import { is, TypedQueryBuilder } from 'drizzle-orm';
function isValidSelect(s: unknown): s is TypedQueryBuilder<any, any> {
return s != null && typeof s === 'object'
&& typeof (s as any).getSelectedFields === 'function';
} Prevention
- Filter falsy/undefined entries out of dynamic set-operator arrays before use.
- Never push conditionally-undefined operators into a setOperators array.
- Treat this error as a signal of corrupted builder state; audit custom forks.
When it happens
Trigger: Produced only when PgSelect.addSetOperators (or a direct dialect call) pushes/derives a setOperators array whose first element is undefined — e.g. calling a set-operator method with an undefined right-hand select, or a custom fork that passes a sparse array to buildSetOperations. Calling .union(undefined) or programmatically appending an undefined operator object can surface it.
Common situations: Almost never seen with the stock public API because createSetOperator and the chainable helpers validate selections first. Encountered mainly in internal/tooling code, monkeypatched builds, or when a dynamic array of selects contains a hole. May appear after a refactor that conditionally pushes operators (push(cond ? op : undefined)).
Related errors
- Cannot pass undefined values to any set operator
- Set operator error (union / intersect / except): selected fi
- Set operator error (union / intersect / except): selected fi
- Cannot pass undefined values to any set operator
- Cannot pass undefined values to any set operator
AI-assisted analysis of drizzle-team/drizzle-orm@b7862528fd (2026-08-03).
Data as JSON: /data/errors/92c071a33c133b5d.json.
Report an issue: GitHub.