can1357/oh-my-pi · error · ToolError

SQLite order column '${column}' not found in table schema

Error message

SQLite order column '${column}' not found in table schema

What it means

Thrown by resolveOrderClause in the sqlite-reader tool when the 'order' query parameter names a column that does not exist in the target table's schema. The tool validates ORDER BY columns against the actual table columns to prevent SQL injection and confusing SQLite errors, since only schema-known identifiers are quoted and accepted.

Source

Thrown at packages/coding-agent/src/tools/sqlite-reader.ts:387

	return key;
}

function resolveOrderClause(order: string | undefined, columns: string[]): string {
	if (!order) return "";
	const trimmed = order.trim();
	if (!trimmed) return "";

	const separatorIndex = trimmed.lastIndexOf(":");
	const column = separatorIndex === -1 ? trimmed : trimmed.slice(0, separatorIndex);
	const direction =
		separatorIndex === -1
			? "asc"
			: trimmed
					.slice(separatorIndex + 1)
					.trim()
					.toLowerCase();
	if (!columns.includes(column)) {
		throw new ToolError(`SQLite order column '${column}' not found in table schema`);
	}
	if (direction !== "asc" && direction !== "desc") {
		throw new ToolError(`SQLite order direction must be 'asc' or 'desc'; got '${direction}'`);
	}
	return ` ORDER BY ${quoteSqliteIdentifier(column)} ${direction.toUpperCase()}`;
}

const FORBIDDEN_WHERE_KEYWORDS = new Set([
	"limit",
	"offset",
	"union",
	"intersect",
	"except",
	"attach",
	"detach",
	"pragma",
]);

View on GitHub (pinned to 9690622007)

Solutions

  1. Inspect the table schema (e.g. PRAGMA table_info or a schema-listing call) and use an existing column name
  2. Fix typos/case in the order parameter to match the schema exactly
  3. Remove the order param to get default row ordering
  4. If the column was renamed in a migration, update the calling code to the new name

Example fix

// before
tool.read('sqlite:users?order=emial:asc')
// after
tool.read('sqlite:users?order=email:asc')
Defensive patterns

Strategy: validation

Validate before calling

const cols = await listTableColumns('users');
if (orderColumn && !cols.includes(orderColumn)) {
  throw new Error(`order column '${orderColumn}' not in [${cols.join(', ')}]`);
}

Type guard

function isValidOrderColumn(col: string, columns: string[]): col is string {
  return columns.includes(col);
}

Try / catch

try {
  await reader.read(`db.sqlite/users?order=${col}:asc`);
} catch (err) {
  if (err instanceof ToolError && err.message.includes("not found in table schema")) {
    // fall back to default ordering or log schema mismatch
  } else throw err;
}

Prevention

When it happens

Trigger: Calling the sqlite read tool with an order param like 'table:users?order=email:asc' where 'email' is not a column of the users table; renaming a column without updating the order param; typos or case mismatches in the column name.

Common situations: Schema drift after a migration added/renamed columns; hard-coded order fields copied from another table; assuming SQLite column names are case-insensitive when the schema check is exact-match.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/9fc40f40e955e5f2. Report an issue: GitHub.