{"id":"084d5d8a07b5b237","repo":"drizzle-team/drizzle-orm","slug":"insert-select-error-selected-fields-are-not-the-s-084d5d","errorCode":null,"errorMessage":"Insert select error: selected fields are not the same or are in a different order compared to the table definition","messagePattern":"Insert select error: selected fields are not the same or are in a different order compared to the table definition","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/mysql-core/query-builders/insert.ts","lineNumber":109,"sourceCode":"\tselect(\n\t\tselectQuery: (qb: QueryBuilder) => MySqlInsertSelectQueryBuilder<TTable>,\n\t): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tselect(selectQuery: (qb: QueryBuilder) => SQL): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tselect(selectQuery: SQL): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tselect(selectQuery: MySqlInsertSelectQueryBuilder<TTable>): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tselect(\n\t\tselectQuery:\n\t\t\t| SQL\n\t\t\t| MySqlInsertSelectQueryBuilder<TTable>\n\t\t\t| ((qb: QueryBuilder) => MySqlInsertSelectQueryBuilder<TTable> | SQL),\n\t): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\tconst select = typeof selectQuery === 'function' ? selectQuery(new QueryBuilder()) : selectQuery;\n\n\t\tif (\n\t\t\t!is(select, SQL)\n\t\t\t&& !haveSameKeys(this.table[Columns], select._.selectedFields)\n\t\t) {\n\t\t\tthrow new Error(\n\t\t\t\t'Insert select error: selected fields are not the same or are in a different order compared to the table definition',\n\t\t\t);\n\t\t}\n\n\t\treturn new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);\n\t}\n}\n\nexport type MySqlInsertWithout<T extends AnyMySqlInsert, TDynamic extends boolean, K extends keyof T & string> =\n\tTDynamic extends true ? T\n\t\t: Omit<\n\t\t\tMySqlInsertBase<\n\t\t\t\tT['_']['table'],\n\t\t\t\tT['_']['queryResult'],\n\t\t\t\tT['_']['preparedQueryHKT'],\n\t\t\t\tT['_']['returning'],\n\t\t\t\tTDynamic,\n\t\t\t\tT['_']['excludedMethods'] | '$returning'","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/mysql-core/query-builders/insert.ts#L91-L127","documentation":"Thrown by MySqlInsertBuilder.select (line 109) when an INSERT...SELECT is built and the select query's selected fields don't match the table's columns exactly (count and order). haveSameKeys enforces that the SELECT projects the same keys in the same order as the target table's columns.","triggerScenarios":"db.insert(table).select(qb => qb.select({ ... }).from(...)) where the selected fields differ in number, names, or order from table[Columns]. Also fires for a raw SQL select if it's not a TypedQueryBuilder with matching selectedFields.","commonSituations":"Selecting a subset of columns; selecting columns in a different order than defined in the table; aliasing columns differently than the table's keys; forgetting computed/generated columns.","solutions":["Select exactly the table's columns in the same order: select every column from the source by its table key name.","If you need a subset, use a separate insert().values() path or align the source table schema to match.","Ensure aliases in the select match the target table column keys (haveSameKeys compares Object.keys order).","For raw SQL, make sure the projection yields the same field set/order as the table definition."],"exampleFix":"// before - subset/mismatch\ndb.insert(users).select(\n  qb => qb.select({ id: source.id, name: source.name }).from(source)\n); // users has more columns => throws\n\n// after - select all columns in order\ndb.insert(users).select(\n  qb => qb.select({ id: source.id, name: source.name, email: source.email, createdAt: source.createdAt }).from(source)\n);","handlingStrategy":"validation","validationCode":"import { haveSameKeys, Columns, Table } from '~/utils.ts';\n// Compare selected fields keys to table columns keys before insert().select()\nconst ok = haveSameKeys(table[Columns], selectQuery._.selectedFields);\nif (!ok) throw new Error('Select projection must match target table columns in order');","typeGuard":"function projectionMatchesTable(table: MySqlTable, sel: { getSelectedFields(): Record<string, unknown> }): boolean {\n  return haveSameKeys(table[Columns], sel.getSelectedFields());\n}","tryCatchPattern":null,"preventionTips":["Project every target column by its exact key, in the same order.","Avoid selecting a subset when using insert().select().","Keep aliases identical to the target table's column keys."],"tags":["mysql","insert","insert-select","validation"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}