yiisoft/yii2 · error · yii\base\InvalidArgumentException
Table not found: $table
Error message
Table not found: $table
What it means
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'.
Source
Thrown at framework/db/mssql/QueryBuilder.php:319
/**
* Builds a SQL command for adding or updating a comment to a table or a column. The command built will check if a comment
* already exists. If so, it will be updated, otherwise, it will be added.
*
* @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.
* @param string $table the table to be commented or whose column is to be commented. The table name will be
* properly quoted by the method.
* @param string|null $column optional. The name of the column to be commented. If empty, the command will add the
* comment to the table instead. The column name will be properly quoted by the method.
* @return string the SQL statement for adding a comment.
* @throws InvalidArgumentException if the table does not exist.
* @since 2.0.24
*/
protected function buildAddCommentSql($comment, $table, $column = null)
{
$tableSchema = $this->db->schema->getTableSchema($table);
if ($tableSchema === null) {
throw new InvalidArgumentException("Table not found: $table");
}
$schemaName = $tableSchema->schemaName ? "N'" . $tableSchema->schemaName . "'" : 'SCHEMA_NAME()';
$tableName = 'N' . $this->db->quoteValue($tableSchema->name);
$columnName = $column ? 'N' . $this->db->quoteValue($column) : null;
$comment = 'N' . $this->db->quoteValue($comment);
$functionParams = "
@name = N'MS_description',
@value = $comment,
@level0type = N'SCHEMA', @level0name = $schemaName,
@level1type = N'TABLE', @level1name = $tableName"
. ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';';
return "
IF NOT EXISTS (
SELECT 1
FROM fn_listextendedproperty (View on GitHub (pinned to 66f00d18a2)
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
Example fix
// before
$this->addCommentOnColumn('product', 'sku', 'Stock keeping unit'); // table not visible -> InvalidArgumentException
// after
if ($this->db->getTableSchema('product') === null) {
$this->db->schema->refresh(); // pick up tables created moments ago
}
if ($this->db->getTableSchema('product') === null) {
throw new \yii\console\Exception('Table product not found on this connection.');
}
$this->addCommentOnColumn('product', 'sku', 'Stock keeping unit'); Defensive patterns
Strategy: validation
Validate before calling
$schema = $db->getTableSchema($table);
if ($schema === null) {
$db->schema->refresh(); // pick up tables created moments ago
$schema = $db->getTableSchema($table);
}
if ($schema === null) {
throw new \InvalidArgumentException("Table '$table' not found on this connection.");
}
$db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute(); Try / catch
try {
$db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute();
} catch (\yii\base\InvalidArgumentException $e) {
\Yii::error('Comment migration failed: ' . $e->getMessage(), __METHOD__);
throw $e; // surface misconfiguration early
} Prevention
- 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
When it happens
Trigger: $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.
Common situations: 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.
Related errors
- Unable to find column '$column' in table '$table'.
- Table not found: $tableName
- There is not sequence associated with table '$tableName'.
- yii\db\mssql\conditions\InConditionBuilder::buildSubqueryInC
- yii\db\cubrid\QueryBuilder::addCheck is not supported by CUB
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/a8c42b693d28abbc.
Report an issue: GitHub.