drizzle-team/drizzle-orm · error · Error

Alias "${tableName}" is already used in this query

Error message

Alias "${tableName}" is already used in this query

What it means

Error "Alias "${tableName}" is already used in this query" thrown in drizzle-team/drizzle-orm.

Source

Thrown at drizzle-orm/src/gel-core/query-builders/select.ts:239

	private createJoin<
		TJoinType extends JoinType,
		TIsLateral extends (TJoinType extends 'full' | 'right' ? false : boolean),
	>(
		joinType: TJoinType,
		lateral: TIsLateral,
	): 'cross' extends TJoinType ? GelSelectCrossJoinFn<this, TDynamic, TIsLateral>
		: GelSelectJoinFn<this, TDynamic, TJoinType, TIsLateral>
	{
		return ((
			table: GelTable | Subquery | GelViewBase | SQL,
			on?: ((aliases: TSelection) => SQL | undefined) | SQL | undefined,
		) => {
			const baseTableName = this.tableName;
			const tableName = getTableLikeName(table);

			if (typeof tableName === 'string' && this.config.joins?.some((join) => join.alias === tableName)) {
				throw new Error(`Alias "${tableName}" is already used in this query`);
			}

			// store all tables used in a query
			for (const item of extractUsedTable(table)) this.usedTables.add(item);

			if (!this.isPartialSelect) {
				// 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
				if (Object.keys(this.joinsNotNullableMap).length === 1 && typeof baseTableName === 'string') {
					this.config.fields = {
						[baseTableName]: this.config.fields,
					};
				}
				if (typeof tableName === 'string' && !is(table, SQL)) {
					const selection = is(table, Subquery)
						? table._.selectedFields
						: is(table, View)
						? table[ViewBaseConfig].selectedFields
						: table[Table.Symbol.Columns];

View on GitHub (pinned to b7862528fd)

Solutions

  1. Use a unique alias for the joined table, e.g. alias(myTable, 'other_name') via the alias() helper from drizzle-orm.
  2. Check for duplicate joins of the same table without distinct aliases.

When it happens

Trigger: Joining or aliasing two tables with the same alias name in one Gel select query.

Common situations: Self-join without aliasing via alias(); reusing an alias variable twice; dynamic joins generating duplicate names.


AI-assisted analysis of drizzle-team/drizzle-orm@b7862528fd (2026-08-03). Data as JSON: /data/errors/69f6bdf55c21e7a7.json. Report an issue: GitHub.