yiisoft/yii2 · error · yii\db\Exception
Unable to find column '$column' in table '$table'.
Error message
Unable to find column '$column' in table '$table'.
What it means
getColumnDefinition() is a private MySQL QueryBuilder helper used by addCommentOnColumn()/dropCommentFromColumn() to rebuild the full column DDL. It runs SHOW CREATE TABLE and throws yii\db\Exception when queryOne() returns no row - in practice the table cannot be read (missing, wrong database, unreadable name). Note the message names the column, but the code path at line 363 is actually a table-level read failure.
Source
Thrown at framework/db/mysql/QueryBuilder.php:364
public function selectExists($rawSql)
{
return 'SELECT EXISTS(' . $rawSql . ') AS ' . $this->db->quoteColumnName('result');
}
/**
* Gets column definition.
*
* @param string $table table name
* @param string $column column name
* @return string|null the column definition
* @throws Exception in case when table does not contain column
*/
private function getColumnDefinition($table, $column)
{
$quotedTable = $this->db->quoteTableName($table);
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryOne();
if ($row === false) {
throw new Exception("Unable to find column '$column' in table '$table'.");
}
if (isset($row['Create Table'])) {
$sql = $row['Create Table'];
} else {
$row = array_values($row);
$sql = $row[1];
}
if (preg_match_all('/^\s*[`"](.*?)[`"]\s+(.*?),?$/m', $sql, $matches)) {
foreach ($matches[1] as $i => $c) {
if ($c === $column) {
return $matches[2][$i];
}
}
}
return null;
}
View on GitHub (pinned to 66f00d18a2)
Solutions
- Verify both names before commenting: isset($db->getTableSchema($table, true)->columns[$column]).
- Move the addCommentOnColumn() call into, or after, the migration that creates the table/column.
- Fix typos in the table or column name.
- Refresh schema cache ($db->schema->refresh()) if introspection data is stale.
Example fix
// before
$this->addCommentOnColumn('user', 'emial', 'Email address');
// after
$columns = $db->getTableSchema('user', true)->columns ?? [];
if (!array_key_exists('email', $columns)) {
throw new InvalidArgumentException("Column 'user.email' does not exist.");
}
$this->addCommentOnColumn('user', 'email', 'Email address'); Defensive patterns
Strategy: validation
Validate before calling
// Before addCommentOnColumn()/dropCommentFromColumn()
$schema = $db->getTableSchema($table, true);
if ($schema === null || !array_key_exists($column, $schema->columns)) {
throw new InvalidArgumentException("'$table.$column' does not exist; cannot comment.");
}
$this->addCommentOnColumn($table, $column, $comment); Try / catch
try {
$migration->addCommentOnColumn($table, $column, $comment);
} catch (\yii\db\Exception $e) {
// SHOW CREATE TABLE returned nothing: table missing/unreadable
// verify environment and table names, then re-run
} Prevention
- Keep comment migrations adjacent to the migration that creates the table/column.
- Lint migration names against the live schema before running in CI.
- Use the same schema snapshot across environments to catch typos early.
When it happens
Trigger: A migration calling $this->addCommentOnColumn('user', 'emial', '...') or dropCommentOnColumn where the table or column name is wrong, the table is created in a later migration, the statement runs against a database where the table was never created, or the target is actually a view.
Common situations: Migration ordering mistakes (comment added before createTable in the chain); typos in table/column names; environment drift where one DB has the table and another does not; SQLite-vs-MySQL fixture databases with different schemas.
Related errors
- Unable to find column '$oldName' in table '$table'.
- Table not found: $tableName
- There is no sequence associated with table '$tableName'.
- MySQL < 8.0.16 does not support check constraints.
- MySQL does not support default value constraints.
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/4e8f57fcea2695dd.
Report an issue: GitHub.