{"record":{"id":"f02104aeae726f81","repo":"yiisoft/yii2","slug":"unable-to-find-column-oldname-in-table-table","errorCode":null,"errorMessage":"Unable to find column '$oldName' in table '$table'.","messagePattern":"Unable to find column '\\$oldName' in table '\\$table'\\.","errorType":"exception","errorClass":"yii\\db\\Exception","httpStatus":null,"severity":"error","filePath":"framework/db/mysql/QueryBuilder.php","lineNumber":85,"sourceCode":"        return array_merge(parent::defaultExpressionBuilders(), [\n            'yii\\db\\JsonExpression' => 'yii\\db\\mysql\\JsonExpressionBuilder',\n        ]);\n    }\n\n    /**\n     * Builds a SQL statement for renaming a column.\n     * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.\n     * @param string $oldName the old name of the column. The name will be properly quoted by the method.\n     * @param string $newName the new name of the column. The name will be properly quoted by the method.\n     * @return string the SQL statement for renaming a DB column.\n     * @throws Exception\n     */\n    public function renameColumn($table, $oldName, $newName)\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 '$oldName' 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 === $oldName) {\n                    return \"ALTER TABLE $quotedTable CHANGE \"\n                        . $this->db->quoteColumnName($oldName) . ' '\n                        . $this->db->quoteColumnName($newName) . ' '\n                        . $matches[2][$i];\n                }\n            }\n        }\n        // try to give back a SQL anyway","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mysql/QueryBuilder.php#L67-L103","documentation":"yii\\db\\mysql\\QueryBuilder::renameColumn() (framework/db/mysql/QueryBuilder.php:80-...) runs SHOW CREATE TABLE to preserve the column's exact definition when emitting ALTER TABLE ... CHANGE. The exception fires when that query returns no row — meaning the table itself does not exist or is unreadable — even though the message blames the column ('Unable to find column X'). Reached via yii\\db\\Command::renameColumn() (framework/db/Command.php:746) and Migration::renameColumn() (framework/db/Migration.php:414), so migrations are the usual place it appears.","triggerScenarios":"$this->renameColumn('usr','name','full_name') in a migration where the table name is misspelled or the table was never created (earlier migration failed); renaming a column in the same migration before createTable(); the migration component bound to a different DB connection than the one holding the table.","commonSituations":"Migration chains reordered/squashed so renameColumn runs before the table exists; multi-connection apps where $this->db points at the wrong database; environments where a prior migration failed silently.","solutions":["Verify the table first: if ($this->db->getTableSchema($table) === null) throw a clear exception","Check migration state with ./yii migrate/history and apply missing base migrations","Confirm the migration targets the right connection: var_dump($this->db->dsn) or set the migration component explicitly"],"exampleFix":"// before\npublic function safeUp()\n{\n    $this->renameColumn('user_profile', 'name', 'full_name'); // table missing -> Exception\n}\n\n// after\npublic function safeUp()\n{\n    if ($this->db->getTableSchema('user_profile') === null) {\n        throw new \\yii\\console\\Exception('Table user_profile does not exist — apply earlier migrations first.');\n    }\n    $this->renameColumn('user_profile', 'name', 'full_name');\n}","handlingStrategy":"validation","validationCode":"if ($this->db->getTableSchema($table) === null) {\n    throw new \\yii\\console\\Exception(\"Table '$table' does not exist — apply earlier migrations first.\");\n}\n$this->renameColumn($table, $oldName, $newName);","typeGuard":null,"tryCatchPattern":"try {\n    $this->renameColumn($table, $oldName, $newName);\n} catch (\\yii\\db\\Exception $e) {\n    // SHOW CREATE TABLE returned no row — verify table name and connection\n    \\Yii::error('renameColumn failed: ' . $e->getMessage(), __METHOD__);\n    throw $e;\n}","preventionTips":["Despite the message naming the column, this exception means SHOW CREATE TABLE returned no row — the table is missing or unreadable","Check ./yii migrate/history before running rename migrations on a new environment","In multi-DB apps, verify $this->db->dsn targets the database that holds the table"],"tags":["yii2","mysql","database","migration","rename-column","schema"],"backgroundTag":"column-not-found","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}