{"id":"c9224c11b9d4a032","repo":"drizzle-team/drizzle-orm","slug":"set-operator-error-union-intersect-except-s-c9224c","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":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/mysql-core/query-builders/select.ts","lineNumber":570,"sourceCode":"\t\trightSelection:\n\t\t\t| ((setOperators: GetMySqlSetOperators) => SetOperatorRightSelect<TValue, TResult>)\n\t\t\t| SetOperatorRightSelect<TValue, TResult>,\n\t) => MySqlSelectWithout<\n\t\tthis,\n\t\tTDynamic,\n\t\tMySqlSetOperatorExcludedMethods,\n\t\ttrue\n\t> {\n\t\treturn (rightSelection) => {\n\t\t\tconst rightSelect = (typeof rightSelection === 'function'\n\t\t\t\t? rightSelection(getMySqlSetOperators())\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":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/mysql-core/query-builders/select.ts#L552-L588","documentation":"Thrown by createSetOperator on the select builder (line 570) when a chained set operator (union/unionAll/intersect/intersectAll/except/exceptAll) is added and the right select's fields don't match the left select's fields (keys + order via haveSameKeys). Set operations require both sides to project the same shape.","triggerScenarios":"db.select({ a, b }).from(t1).union(db.select({ a, c }).from(t2)) - different keys or order. The method-style operator validates against this.getSelectedFields() vs rightSelect.getSelectedFields().","commonSituations":"Different columns selected on each side; same columns in different order; one side aliased differently; refactored one select but not the other.","solutions":["Make both selects project identical keys in the same order.","Alias fields on both sides so the key sets match exactly.","If you need different shapes, combine them with a different query strategy (e.g. client-side merge).","Double-check that aliased columns use the same alias key on both sides."],"exampleFix":"// before\ndb.select({ id: users.id, name: users.name }).from(users)\n  .union(db.select({ id: customers.id, email: customers.email }).from(customers));\n// keys {id,name} vs {id,email} => throws\n\n// after - align shapes\ndb.select({ id: users.id, label: users.name }).from(users)\n  .union(db.select({ id: customers.id, label: customers.email }).from(customers));","handlingStrategy":"validation","validationCode":"import { haveSameKeys } from '~/utils.ts';\nif (!haveSameKeys(left.getSelectedFields(), right.getSelectedFields())) {\n  throw new Error('Set-operator sides must select the same keys in the same order');\n}","typeGuard":"function shapesMatch(a: { getSelectedFields(): Record<string, unknown> }, b: { getSelectedFields(): Record<string, unknown> }): boolean {\n  return haveSameKeys(a.getSelectedFields(), b.getSelectedFields());\n}","tryCatchPattern":null,"preventionTips":["Project identical keys in the same order on both sides of a set operator.","Use consistent aliases across all operands.","Re-check shapes after refactoring any select used in a union/intersect/except."],"tags":["mysql","set-operators","union","validation","select"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}