{"record":{"id":"4e8f57fcea2695dd","repo":"yiisoft/yii2","slug":"unable-to-find-column-column-in-table-table-4e8f57","errorCode":null,"errorMessage":"Unable to find column '$column' in table '$table'.","messagePattern":"Unable to find column '\\$column' in table '\\$table'\\.","errorType":"exception","errorClass":"yii\\db\\Exception","httpStatus":null,"severity":"error","filePath":"framework/db/mysql/QueryBuilder.php","lineNumber":364,"sourceCode":"    public function selectExists($rawSql)\n    {\n        return 'SELECT EXISTS(' . $rawSql . ') AS ' . $this->db->quoteColumnName('result');\n    }\n\n    /**\n     * Gets column definition.\n     *\n     * @param string $table table name\n     * @param string $column column name\n     * @return string|null the column definition\n     * @throws Exception in case when table does not contain column\n     */\n    private function getColumnDefinition($table, $column)\n    {\n        $quotedTable = $this->db->quoteTableName($table);\n        $row = $this->db->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryOne();\n        if ($row === false) {\n            throw new Exception(\"Unable to find column '$column' in table '$table'.\");\n        }\n        if (isset($row['Create Table'])) {\n            $sql = $row['Create Table'];\n        } else {\n            $row = array_values($row);\n            $sql = $row[1];\n        }\n        if (preg_match_all('/^\\s*[`\"](.*?)[`\"]\\s+(.*?),?$/m', $sql, $matches)) {\n            foreach ($matches[1] as $i => $c) {\n                if ($c === $column) {\n                    return $matches[2][$i];\n                }\n            }\n        }\n\n        return null;\n    }\n","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mysql/QueryBuilder.php#L346-L382","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$this->addCommentOnColumn('user', 'emial', 'Email address');\n\n// after\n$columns = $db->getTableSchema('user', true)->columns ?? [];\nif (!array_key_exists('email', $columns)) {\n    throw new InvalidArgumentException(\"Column 'user.email' does not exist.\");\n}\n$this->addCommentOnColumn('user', 'email', 'Email address');","handlingStrategy":"validation","validationCode":"// Before addCommentOnColumn()/dropCommentFromColumn()\n$schema = $db->getTableSchema($table, true);\nif ($schema === null || !array_key_exists($column, $schema->columns)) {\n    throw new InvalidArgumentException(\"'$table.$column' does not exist; cannot comment.\");\n}\n$this->addCommentOnColumn($table, $column, $comment);","typeGuard":null,"tryCatchPattern":"try {\n    $migration->addCommentOnColumn($table, $column, $comment);\n} catch (\\yii\\db\\Exception $e) {\n    // SHOW CREATE TABLE returned nothing: table missing/unreadable\n    // verify environment and table names, then re-run\n}","preventionTips":["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."],"tags":["yii2","php","mysql","database","migration","column-not-found"],"backgroundTag":"database-column-not-found","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}