{"id":"26c5f5db1eb5e4f1","repo":"drizzle-team/drizzle-orm","slug":"set-operator-error-union-intersect-except-s-26c5f5","errorCode":null,"errorMessage":"Set operator error (union / intersect / except): selected fields are not the same or are in a different order","messagePattern":"Set operator error \\(union / intersect / except\\): selected fields are not the same or are in a different order","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/singlestore-core/query-builders/select.ts","lineNumber":502,"sourceCode":"\t\trightSelection:\n\t\t\t| ((setOperators: GetSingleStoreSetOperators) => SetOperatorRightSelect<TValue, TResult>)\n\t\t\t| SetOperatorRightSelect<TValue, TResult>,\n\t) => SingleStoreSelectWithout<\n\t\tthis,\n\t\tTDynamic,\n\t\tSingleStoreSetOperatorExcludedMethods,\n\t\ttrue\n\t> {\n\t\treturn (rightSelection) => {\n\t\t\tconst rightSelect = (typeof rightSelection === 'function'\n\t\t\t\t? rightSelection(getSingleStoreSetOperators())\n\t\t\t\t: rightSelection) as TypedQueryBuilder<\n\t\t\t\t\tany,\n\t\t\t\t\tTResult\n\t\t\t\t>;\n\n\t\t\tif (!haveSameKeys(this.getSelectedFields(), rightSelect.getSelectedFields())) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'Set operator error (union / intersect / except): selected fields are not the same or are in a different order',\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthis.config.setOperators.push({ type, isAll, rightSelect });\n\t\t\treturn this as any;\n\t\t};\n\t}\n\n\t/**\n\t * Adds `union` set operator to the query.\n\t *\n\t * Calling this method will combine the result sets of the `select` statements and remove any duplicate rows that appear across them.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/set-operations#union}\n\t *\n\t * @example\n\t *","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/singlestore-core/query-builders/select.ts#L484-L520","documentation":"Thrown in createSetOperator (select.ts:502) for the method-chained form (.union/.unionAll/.intersect/.except/.minus). Before pushing the operator it compares the keys of the left and right selections via haveSameKeys; SQL set operators require both sides to project identical columns in the same order, so a mismatch is rejected at build time.","triggerScenarios":"Calling qb.union(otherQb) where qb selects {a, b} and otherQb selects {a, c} or selects the same columns in a different key order; selecting computed/aliased fields whose keys differ between branches.","commonSituations":"Refactoring one side of a union and changing its select shape; combining selects from tables with different column subsets expecting them to align; ordering object keys differently across two builders.","solutions":["Align both selects to the exact same set of keys in the same order, ideally by projecting explicit aliases.","Use sql`...` aliases to normalize differing source columns to a common output key.","Extract the shared selection shape into a constant and reuse it on both sides."],"exampleFix":"// before\nawait db.select({ id: users.id }).from(users)\n  .union(db.select({ name: customers.name }).from(customers));\n// after\nawait db.select({ label: users.id }).from(users)\n  .union(db.select({ label: customers.name }).from(customers));","handlingStrategy":"validation","validationCode":"import { haveSameKeys } from 'drizzle-orm/utils';\n// both args are select builders\nfunction assertSameSelectionKeys(left, right) {\n  if (!haveSameKeys(left.getSelectedFields(), right.getSelectedFields())) {\n    throw new Error('Union branches must select the same keys in the same order');\n  }\n}","typeGuard":"function sameSelectionShape(left, right) {\n  return haveSameKeys(left.getSelectedFields(), right.getSelectedFields());\n}","tryCatchPattern":null,"preventionTips":["Project explicit aliases on both sides of a set operator.","Define a shared selection-fields constant for both branches.","Keep key order identical across union/intersect/except branches."],"tags":["set-operators","union","singlestore","schema"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}