{"record":{"id":"a8c42b693d28abbc","repo":"yiisoft/yii2","slug":"table-not-found-table","errorCode":null,"errorMessage":"Table not found: $table","messagePattern":"Table not found: \\$table","errorType":"exception","errorClass":"yii\\base\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"framework/db/mssql/QueryBuilder.php","lineNumber":319,"sourceCode":"     /**\n      * Builds a SQL command for adding or updating a comment to a table or a column. The command built will check if a comment\n      * already exists. If so, it will be updated, otherwise, it will be added.\n      *\n      * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.\n      * @param string $table the table to be commented or whose column is to be commented. The table name will be\n      * properly quoted by the method.\n      * @param string|null $column optional. The name of the column to be commented. If empty, the command will add the\n      * comment to the table instead. The column name will be properly quoted by the method.\n      * @return string the SQL statement for adding a comment.\n      * @throws InvalidArgumentException if the table does not exist.\n      * @since 2.0.24\n      */\n    protected function buildAddCommentSql($comment, $table, $column = null)\n    {\n        $tableSchema = $this->db->schema->getTableSchema($table);\n\n        if ($tableSchema === null) {\n            throw new InvalidArgumentException(\"Table not found: $table\");\n        }\n\n        $schemaName = $tableSchema->schemaName ? \"N'\" . $tableSchema->schemaName . \"'\" : 'SCHEMA_NAME()';\n        $tableName = 'N' . $this->db->quoteValue($tableSchema->name);\n        $columnName = $column ? 'N' . $this->db->quoteValue($column) : null;\n        $comment = 'N' . $this->db->quoteValue($comment);\n\n        $functionParams = \"\n            @name = N'MS_description',\n            @value = $comment,\n            @level0type = N'SCHEMA', @level0name = $schemaName,\n            @level1type = N'TABLE', @level1name = $tableName\"\n            . ($column ? \", @level2type = N'COLUMN', @level2name = $columnName\" : '') . ';';\n\n        return \"\n            IF NOT EXISTS (\n                    SELECT 1\n                    FROM fn_listextendedproperty (","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mssql/QueryBuilder.php#L301-L337","documentation":"yii\\db\\mssql\\QueryBuilder::buildAddCommentSql() (framework/db/mssql/QueryBuilder.php:319, since 2.0.24) backs addCommentOnColumn()/addCommentOnTable() and resolves the table via $this->db->schema->getTableSchema($table) because it needs the real schemaName and name to call sp_addextendedproperty with N'' parameters. A null schema — table unknown to the connection — throws InvalidArgumentException 'Table not found'.","triggerScenarios":"$this->addCommentOnColumn('produt','sku','SKU') with a typo; commenting a table created earlier in the same migration run while the schema cache still holds pre-create metadata; referencing a table in another schema without the prefix; running the migration on the wrong DB connection.","commonSituations":"Comment migrations added after the fact (since 2.0.24); long-running processes with an enabled schema cache that do not see freshly created tables; multi-connection apps where $this->db in the migration is not the connection holding the table.","solutions":["Verify first: if ($db->getTableSchema($table) === null) fail fast with a clear message","Refresh metadata when the cache may be stale: $db->schema->refresh() before the comment call","Use the fully qualified name (e.g. 'dbo.product') and confirm the migration's connection component"],"exampleFix":"// before\n$this->addCommentOnColumn('product', 'sku', 'Stock keeping unit'); // table not visible -> InvalidArgumentException\n\n// after\nif ($this->db->getTableSchema('product') === null) {\n    $this->db->schema->refresh(); // pick up tables created moments ago\n}\nif ($this->db->getTableSchema('product') === null) {\n    throw new \\yii\\console\\Exception('Table product not found on this connection.');\n}\n$this->addCommentOnColumn('product', 'sku', 'Stock keeping unit');","handlingStrategy":"validation","validationCode":"$schema = $db->getTableSchema($table);\nif ($schema === null) {\n    $db->schema->refresh(); // pick up tables created moments ago\n    $schema = $db->getTableSchema($table);\n}\nif ($schema === null) {\n    throw new \\InvalidArgumentException(\"Table '$table' not found on this connection.\");\n}\n$db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute();","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute();\n} catch (\\yii\\base\\InvalidArgumentException $e) {\n    \\Yii::error('Comment migration failed: ' . $e->getMessage(), __METHOD__);\n    throw $e; // surface misconfiguration early\n}","preventionTips":["Always resolve the table via getTableSchema() before MSSQL comment operations — the builder needs schemaName anyway","Refresh the schema cache when commenting tables created in the same process","Pin migrations to the correct DB connection component in multi-DB apps"],"tags":["yii2","mssql","sqlserver","database","column-comment","migration","extended-property"],"backgroundTag":"table-not-found","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}