drizzle-team/drizzle-orm · error · Error

Your "${f.path.join('->')}" field references a column "${tab

Error message

Your "${f.path.join('->')}" field references a column "${tableName}"."${f.field.name}", but the table "${tableName}" is not part of the query! Did you forget to join it?

What it means

Thrown by MySqlDialect.buildSelectQuery (line 353) when a selected field references a column whose table is neither the main FROM table nor present in any join. Drizzle validates field-to-table provenance at SQL build time to prevent generating invalid cross-table references.

Source

Thrown at drizzle-orm/src/mysql-core/dialect.ts:353

				&& getTableName(f.field.table)
					!== (is(table, Subquery)
						? table._.alias
						: is(table, MySqlViewBase)
						? table[ViewBaseConfig].name
						: is(table, SQL)
						? undefined
						: getTableName(table))
				&& !((table) =>
					joins?.some(
						({ alias }) =>
							alias
								=== (table[Table.Symbol.IsAlias]
									? getTableName(table)
									: table[Table.Symbol.BaseName]),
					))(f.field.table)
			) {
				const tableName = getTableName(f.field.table);
				throw new Error(
					`Your "${
						f.path.join(
							'->',
						)
					}" field references a column "${tableName}"."${f.field.name}", but the table "${tableName}" is not part of the query! Did you forget to join it?`,
				);
			}
		}

		const isSingleTable = !joins || joins.length === 0;

		const withSql = this.buildWithCTE(withList);

		const distinctSql = distinct ? sql` distinct` : undefined;

		const selection = this.buildSelection(fieldsList, { isSingleTable });

		const tableSql = (() => {

View on GitHub (pinned to b7862528fd)

Solutions

  1. Add the missing join: .leftJoin(otherTable, eq(mainTable.id, otherTable.mainId)).
  2. If you only need the column from the other table, ensure that table is in the FROM/JOIN list.
  3. Re-check that the column reference (e.g. users.id) belongs to a table actually present in the query.
  4. Use aliased tables consistently - don't reference the original table name if you aliased it.

Example fix

// before
db.select({ name: users.name, petName: pets.name })
  .from(users); // pets not joined

// after
db.select({ name: users.name, petName: pets.name })
  .from(users)
  .leftJoin(pets, eq(users.id, pets.ownerId));
Defensive patterns

Strategy: validation

Validate before calling

// Before building, ensure every referenced table is joined
const joinedAliases = new Set([mainTableName, ...joins.map((j) => j.alias)]);
for (const col of selectedColumns) {
  if (!joinedAliases.has(getTableName(col.table))) {
    throw new Error(`Missing join for table ${getTableName(col.table)}`);
  }
}

Type guard

function isColumnInQuery(col: MySqlColumn, fromTable: string, joins: { alias: string }[]): boolean {
  const t = getTableName(col.table);
  return t === fromTable || joins.some((j) => j.alias === t);
}

Prevention

When it happens

Trigger: In db.select({ x: otherTable.col }).from(mainTable) without a join on otherTable; or selecting a column from a table alias that was never joined. The loop over fieldsList detects the orphan column.

Common situations: Forgetting to add .leftJoin/.innerJoin for a table whose column you reference in select/where; copy-paste from another query that included a join; referencing a column by the wrong table reference after aliasing.

Related errors


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