drizzle-team/drizzle-orm · error · DrizzleError

No fields selected for table "${tableConfig.tsName}" ("${tab

Error message

No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")

What it means

Thrown by MySqlDialect.buildRelationalQuery (line 884, lateral variant) when the computed selection for a nested relational query is empty. Reached via the RQB API (db.query.table.findMany/findFirst) when the combination of columns/with/extras yields nothing to select.

Source

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

					on: sql`true`,
					table: new Subquery(builtRelation.sql as SQL, {}, relationTableAlias),
					alias: relationTableAlias,
					joinType: 'left',
					lateral: true,
				});
				selection.push({
					dbKey: selectedRelationTsKey,
					tsKey: selectedRelationTsKey,
					field,
					relationTableTsKey: relationTableTsName,
					isJson: true,
					selection: builtRelation.selection,
				});
			}
		}

		if (selection.length === 0) {
			throw new DrizzleError({
				message: `No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")`,
			});
		}

		let result;

		where = and(joinOn, where);

		if (nestedQueryRelation) {
			let field = sql`json_array(${
				sql.join(
					selection.map(({ field, tsKey, isJson }) =>
						isJson
							? sql`${sql.identifier(`${tableAlias}_${tsKey}`)}.${sql.identifier('data')}`
							: is(field, SQL.Aliased)
							? field.sql
							: field
					),

View on GitHub (pinned to b7862528fd)

Solutions

  1. Set columns to undefined/omit it to select all columns, or ensure at least one column is true (include) or not all are false (exclude).
  2. Add at least one `with` relation or one `extras` entry if you genuinely want a minimal selection.
  3. If using a dynamic columns object, validate that at least one field is selected before calling the query.

Example fix

// before
db.query.users.findFirst({
  columns: { id: false, name: false, email: false }, // all excluded => empty
});

// after - omit columns to select all, or include at least one
db.query.users.findFirst({ columns: { id: true } });
// or
db.query.users.findFirst({}); // selects all columns
Defensive patterns

Strategy: validation

Validate before calling

function hasAtLeastOneField(cfg: { columns?: Record<string, boolean>; with?: object; extras?: object }): boolean {
  const colValues = cfg.columns ? Object.values(cfg.columns) : [];
  const anyIncluded = cfg.columns == null || colValues.some(Boolean) || colValues.some((v) => v === false && !colValues.every((x) => x === false));
  return cfg.columns == null || colValues.includes(true) || (colValues.some((v) => v === false) && !colValues.every((v) => v === false)) || Boolean(cfg.with) || Boolean(cfg.extras);
}

Type guard

function querySelectsAnything(cfg: { columns?: Record<string, boolean>; with?: object; extras?: object }): boolean {
  if (!cfg.columns) return true; // selects all
  const vals = Object.values(cfg.columns);
  if (vals.every((v) => v === false)) return Boolean(cfg.with) || Boolean(cfg.extras);
  return true;
}

Prevention

When it happens

Trigger: Using db.query.X.findFirst({ columns: { ... } }) where every column is set to false (exclude mode) and there are no `with` relations and no `extras`. After filtering, selectedColumns is empty and no relations/extras are added, so selection.length === 0.

Common situations: Misunderstanding the columns config as include-only and setting all to false; programmatically building a columns object that resolves to all-false; querying a relation but the relation config produced no fields.

Related errors


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