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

yii\db\cubrid\QueryBuilder::getColumnDefinition() (framework/db/cubrid/QueryBuilder.php:273) powers addCommentOnColumn()/dropCommentFromColumn() (framework/db/cubrid/QueryBuilder.php:220,247) by running SHOW CREATE TABLE and parsing column definitions. The 'Unable to find column' exception fires when that query returns false — meaning the table itself could not be read (usually it does not exist or is not visible to the connection), even though the message names the column. So despite the wording, this is a missing/unreadable table, not necessarily a missing column.

Source

Thrown at framework/db/cubrid/QueryBuilder.php:273

    {
        return $this->addCommentOnTable($table, '');
    }


    /**
     * 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
     * @since 2.0.8
     */
    private function getColumnDefinition($table, $column)
    {
        $row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->db->quoteTableName($table))->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];
        }
        $sql = preg_replace('/^[^(]+\((.*)\).*$/', '\1', $sql);
        $sql = str_replace(', [', ",\n[", $sql);
        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

  1. Verify the table first: if ($db->getTableSchema($table) === null) fail fast with an explicit message
  2. Fix migration ordering so comment statements run after createTable/addColumn
  3. Confirm the connection targets the right database and the user can execute SHOW CREATE TABLE

Example fix

// before
$this->addCommentOnColumn('produt', 'sku', 'SKU'); // table name typo -> Exception

// after
if ($this->db->getTableSchema('product') === null) {
    throw new \yii\console\Exception('Table product does not exist; run base migrations first.');
}
$this->addCommentOnColumn('product', 'sku', 'SKU');
Defensive patterns

Strategy: validation

Validate before calling

$schema = $db->getTableSchema($table);
if ($schema === null) {
    throw new \InvalidArgumentException("Table '$table' not found — cannot comment columns.");
}
if (!isset($schema->columns[$column])) {
    throw new \InvalidArgumentException("Column '$column' not found in '$table'.");
}
$db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute();

Try / catch

try {
    $db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute();
} catch (\yii\db\Exception $e) {
    \Yii::error('addCommentOnColumn failed (table unreadable?): ' . $e->getMessage(), __METHOD__);
}

Prevention

When it happens

Trigger: $db->createCommand()->addCommentOnColumn('missing_table','name','comment')->execute(); a migration commenting columns before the table was created (ordering issue); wrong connection/database; a CUBRID user lacking permission to run SHOW CREATE TABLE.

Common situations: Migration ordering after refactors that create tables in later migrations; typos in table names; environment drift where the table exists in dev but not the target database.

Related errors


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/86096b1d8edcb608. Report an issue: GitHub.