{"id":"69f6bdf55c21e7a7","repo":"drizzle-team/drizzle-orm","slug":"alias-tablename-is-already-used-in-this-query","errorCode":null,"errorMessage":"Alias \"${tableName}\" is already used in this query","messagePattern":"Alias \"(.+?)\" is already used in this query","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/gel-core/query-builders/select.ts","lineNumber":239,"sourceCode":"\n\tprivate createJoin<\n\t\tTJoinType extends JoinType,\n\t\tTIsLateral extends (TJoinType extends 'full' | 'right' ? false : boolean),\n\t>(\n\t\tjoinType: TJoinType,\n\t\tlateral: TIsLateral,\n\t): 'cross' extends TJoinType ? GelSelectCrossJoinFn<this, TDynamic, TIsLateral>\n\t\t: GelSelectJoinFn<this, TDynamic, TJoinType, TIsLateral>\n\t{\n\t\treturn ((\n\t\t\ttable: GelTable | Subquery | GelViewBase | SQL,\n\t\t\ton?: ((aliases: TSelection) => SQL | undefined) | SQL | undefined,\n\t\t) => {\n\t\t\tconst baseTableName = this.tableName;\n\t\t\tconst tableName = getTableLikeName(table);\n\n\t\t\tif (typeof tableName === 'string' && this.config.joins?.some((join) => join.alias === tableName)) {\n\t\t\t\tthrow new Error(`Alias \"${tableName}\" is already used in this query`);\n\t\t\t}\n\n\t\t\t// store all tables used in a query\n\t\t\tfor (const item of extractUsedTable(table)) this.usedTables.add(item);\n\n\t\t\tif (!this.isPartialSelect) {\n\t\t\t\t// If this is the first join and this is not a partial select and we're not selecting from raw SQL, \"move\" the fields from the main table to the nested object\n\t\t\t\tif (Object.keys(this.joinsNotNullableMap).length === 1 && typeof baseTableName === 'string') {\n\t\t\t\t\tthis.config.fields = {\n\t\t\t\t\t\t[baseTableName]: this.config.fields,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tif (typeof tableName === 'string' && !is(table, SQL)) {\n\t\t\t\t\tconst selection = is(table, Subquery)\n\t\t\t\t\t\t? table._.selectedFields\n\t\t\t\t\t\t: is(table, View)\n\t\t\t\t\t\t? table[ViewBaseConfig].selectedFields\n\t\t\t\t\t\t: table[Table.Symbol.Columns];","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/gel-core/query-builders/select.ts#L221-L257","documentation":"Error \"Alias \"${tableName}\" is already used in this query\" thrown in drizzle-team/drizzle-orm.","triggerScenarios":"Joining or aliasing two tables with the same alias name in one Gel select query.","commonSituations":"Self-join without aliasing via alias(); reusing an alias variable twice; dynamic joins generating duplicate names.","solutions":["Use a unique alias for the joined table, e.g. alias(myTable, 'other_name') via the alias() helper from drizzle-orm.","Check for duplicate joins of the same table without distinct aliases."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}