{"id":"f869d4459c0c6ee4","repo":"drizzle-team/drizzle-orm","slug":"insert-select-error-selected-fields-are-not-the-s-f869d4","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":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/sqlite-core/query-builders/insert.ts","lineNumber":100,"sourceCode":"\tselect(\n\t\tselectQuery: (qb: QueryBuilder) => SQLiteInsertSelectQueryBuilder<TTable>,\n\t): SQLiteInsertBase<TTable, TResultType, TRunResult>;\n\tselect(selectQuery: (qb: QueryBuilder) => SQL): SQLiteInsertBase<TTable, TResultType, TRunResult>;\n\tselect(selectQuery: SQL): SQLiteInsertBase<TTable, TResultType, TRunResult>;\n\tselect(selectQuery: SQLiteInsertSelectQueryBuilder<TTable>): SQLiteInsertBase<TTable, TResultType, TRunResult>;\n\tselect(\n\t\tselectQuery:\n\t\t\t| SQL\n\t\t\t| SQLiteInsertSelectQueryBuilder<TTable>\n\t\t\t| ((qb: QueryBuilder) => SQLiteInsertSelectQueryBuilder<TTable> | SQL),\n\t): SQLiteInsertBase<TTable, TResultType, TRunResult> {\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 SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);\n\t}\n}\n\nexport type SQLiteInsertWithout<T extends AnySQLiteInsert, TDynamic extends boolean, K extends keyof T & string> =\n\tTDynamic extends true ? T\n\t\t: Omit<\n\t\t\tSQLiteInsertBase<\n\t\t\t\tT['_']['table'],\n\t\t\t\tT['_']['resultType'],\n\t\t\t\tT['_']['runResult'],\n\t\t\t\tT['_']['returning'],\n\t\t\t\tTDynamic,\n\t\t\t\tT['_']['excludedMethods'] | K","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/sqlite-core/query-builders/insert.ts#L82-L118","documentation":"Thrown by SQLiteInsertBuilder.select() (insert.ts:100) when the SELECT used for INSERT ... SELECT does not expose exactly the same field keys in the same order as the target table's columns. Drizzle compares keys via haveSameKeys (order-sensitive), so a different set of columns, a different order, or extra/missing aliases all fail.","triggerScenarios":"db.insert(t).select(qb => qb.select({...})) where the selected fields do not mirror the table's column list; selecting a subset; selecting in a different key order; using an aliased subquery whose selectedFields differ from the table. The check is skipped only when the select argument is a raw SQL object.","commonSituations":"Copying a subset of columns from a source table; reordering fields in a refactor; adding a new column to the target table without updating the select; using computed fields not present on the target.","solutions":["Make the SELECT output match the target table's columns exactly, in the same declaration order.","If you need a subset, project missing columns as sql`null` (or a default) so the key set and order align.","Pass a raw SQL expression (sql`...`) instead of a builder to bypass the structural check when order/shape differs intentionally."],"exampleFix":"// before — missing 'createdAt'\ndb.insert(users).select((qb) => qb.select({\n  id: sourceUsers.id,\n  name: sourceUsers.name,\n}).from(sourceUsers));\n\n// after — match target columns, same order\ndb.insert(users).select((qb) => qb.select({\n  id: sourceUsers.id,\n  name: sourceUsers.name,\n  createdAt: sql`now()`,\n}).from(sourceUsers));","handlingStrategy":"validation","validationCode":"function assertInsertSelectShape(table, selectedFields) {\n  const tableKeys = Object.keys(table[Symbol.for('drizzle:Columns')]);\n  const selectKeys = Object.keys(selectedFields);\n  if (tableKeys.length !== selectKeys.length\n      || tableKeys.some((k, i) => k !== selectKeys[i])) {\n    throw new Error('SELECT shape does not match target table columns/order');\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Mirror the target table's column declaration order in the SELECT projection.","When adding a target column, update every INSERT...SELECT immediately.","Project missing columns as sql`null` rather than omitting them."],"tags":["insert","insert-select","query-builder","schema"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}