{"record":{"id":"4c567583d4905fac","repo":"yiisoft/yii2","slug":"there-is-not-sequence-associated-with-table-tabl-4c5675","errorCode":null,"errorMessage":"There is not sequence associated with table '$tableName'.","messagePattern":"There is not sequence associated with table '\\$tableName'\\.","errorType":"exception","errorClass":"yii\\base\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"framework/db/mssql/QueryBuilder.php","lineNumber":272,"sourceCode":"                $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;\n        $tableNames = $this->db->getTableSchema($table) ? [$table] : $dbSchema->getTableNames($schema);\n        $viewNames = $dbSchema->getViewNames($schema);\n        $tableNames = array_diff($tableNames, $viewNames);","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mssql/QueryBuilder.php#L254-L290","documentation":"The second failure mode of yii\\db\\mssql\\QueryBuilder::resetSequence() (framework/db/mssql/QueryBuilder.php:272): the table exists (getTableSchema returned a schema) but has no IDENTITY column, i.e. $table->sequenceName is null. DBCC CHECKIDENT can only reseed IDENTITY columns, so calling resetSequence on a table whose primary key is not an IDENTITY property throws InvalidArgumentException 'There is not sequence associated with table'.","triggerScenarios":"$db->createCommand()->resetSequence('audit_log')->execute() where audit_log's PK is a plain INT (or GUID) without IDENTITY; generic fixture code resetting every table; sequences in SQL Server 2012+ (CREATE SEQUENCE) are unrelated — this driver path is IDENTITY-only.","commonSituations":"Fixture base classes looping over all tables; legacy tables with application-generated keys; porting data where the IDENTITY property was dropped.","solutions":["Skip tables without an identity: check $db->getTableSchema($t)->sequenceName !== null before resetting","If the table should auto-generate keys, alter the PK column to use IDENTITY(1,1), then retry","Restrict fixture resets to the identity-backed tables that actually need reseeding"],"exampleFix":"// before\nforeach ($tables as $t) {\n    $db->createCommand()->resetSequence($t)->execute(); // throws on non-identity tables\n}\n\n// after\nforeach ($tables as $t) {\n    $schema = $db->getTableSchema($t);\n    if ($schema !== null && $schema->sequenceName !== null) {\n        $db->createCommand()->resetSequence($t)->execute();\n    }\n}","handlingStrategy":"validation","validationCode":"$schema = $db->getTableSchema($table);\nif ($schema !== null && $schema->sequenceName !== null) {\n    $db->createCommand()->resetSequence($table)->execute();\n} // else: no IDENTITY column -> DBCC CHECKIDENT not applicable","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->resetSequence($table)->execute();\n} catch (\\yii\\db\\Exception $e) {\n    \\Yii::warning(\"Skipped sequence reset for '$table': \" . $e->getMessage(), __METHOD__);\n}","preventionTips":["Treat schema->sequenceName as the identity-column capability check before resetSequence on MSSQL","Restrict fixture resets to identity-backed tables","SQL Server CREATE SEQUENCE objects are unrelated to this IDENTITY-only code path"],"tags":["yii2","mssql","sqlserver","database","sequence","identity","fixture"],"backgroundTag":"missing-database-sequence","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}