{"record":{"id":"1e0910785b77f7eb","repo":"yiisoft/yii2","slug":"table-not-found-tablename-1e0910","errorCode":null,"errorMessage":"Table not found: $tableName","messagePattern":"Table not found: \\$tableName","errorType":"exception","errorClass":"yii\\base\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"framework/db/mssql/QueryBuilder.php","lineNumber":269,"sourceCode":"\n            if ($value === null || $value === 1) {\n                $key = $this->db->quoteColumnName(reset($table->primaryKey));\n                $subSql = (new Query())\n                    ->select('last_value')\n                    ->from('sys.identity_columns')\n                    ->where(['object_id' => new Expression(\"OBJECT_ID('{$tableName}')\")])\n                    ->andWhere(['IS NOT', 'last_value', null])\n                    ->createCommand($this->db)\n                    ->getRawSql();\n                $sql = \"SELECT COALESCE(MAX({$key}), CASE WHEN EXISTS({$subSql}) THEN 0 ELSE 1 END) FROM {$tableName}\";\n                $value = $this->db->createCommand($sql)->queryScalar();\n            } else {\n                $value = (int) $value;\n            }\n\n            return \"DBCC CHECKIDENT ('{$tableName}', RESEED, {$value})\";\n        } elseif ($table === null) {\n            throw new InvalidArgumentException(\"Table not found: $tableName\");\n        }\n\n        throw new InvalidArgumentException(\"There is not sequence associated with table '$tableName'.\");\n    }\n\n    /**\n     * Builds a SQL statement for enabling or disabling integrity check.\n     * @param bool $check whether to turn on or off the integrity check.\n     * @param string $schema the schema of the tables.\n     * @param string $table the table name.\n     * @return string the SQL statement for checking integrity\n     */\n    public function checkIntegrity($check = true, $schema = '', $table = '')\n    {\n        /** @var Schema $dbSchema */\n        $dbSchema = $this->db->getSchema();\n        $enable = $check ? 'CHECK' : 'NOCHECK';\n        $schema = $schema ?: $dbSchema->defaultSchema;","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mssql/QueryBuilder.php#L251-L287","documentation":"yii\\db\\mssql\\QueryBuilder::resetSequence() (framework/db/mssql/QueryBuilder.php:246-272, implemented since 2.0.13) emits DBCC CHECKIDENT to reseed a table's IDENTITY counter, and starts by resolving the table via getTableSchema($tableName). A null schema means SQL Server has no such table from Yii's perspective and InvalidArgumentException 'Table not found' is thrown. It is reached through yii\\db\\Command::resetSequence() (framework/db/Command.php:971), commonly used after truncateTable() or when loading fixtures.","triggerScenarios":"$db->createCommand()->resetSequence('order_items')->execute() where the table is missing or misnamed (including forgotten schema prefix like 'dbo.'); fixture teardown resetting sequences for tables a previous failed migration never created; stale schema cache after out-of-band DDL.","commonSituations":"Fixtures/tests that truncate and reseed identity tables; environments where migrations are partially applied; tables living in a non-default SQL Server schema referenced without the prefix.","solutions":["Verify the fully qualified name first: if ($db->getTableSchema($tableName) === null) fail or skip with a log line","Ensure migrations ran (./yii migrate/history) before running fixture/seed jobs","Refresh cached metadata: $db->schema->refresh() or clear the cache component"],"exampleFix":"// before\n$db->createCommand()->resetSequence('order')->execute(); // throws if table missing\n\n// after\nif ($db->getTableSchema('dbo.order') !== null) {\n    $db->createCommand()->resetSequence('dbo.order')->execute();\n}","handlingStrategy":"validation","validationCode":"if ($db->getTableSchema($tableName) === null) {\n    throw new \\InvalidArgumentException(\"Cannot reset sequence: table '$tableName' does not exist.\");\n}\n$db->createCommand()->resetSequence($tableName)->execute();","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->resetSequence($tableName)->execute();\n} catch (\\yii\\db\\Exception $e) {\n    \\Yii::warning('resetSequence failed: ' . $e->getMessage(), __METHOD__);\n}","preventionTips":["Use fully qualified names ('dbo.table') for non-default SQL Server schemas","Verify migrations are applied before running fixture/seed jobs","Refresh schema metadata after out-of-band DDL: $db->schema->refresh()"],"tags":["yii2","mssql","sqlserver","database","sequence","fixture"],"backgroundTag":"table-not-found","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}