{"record":{"id":"a555d850d5cfbc1a","repo":"yiisoft/yii2","slug":"there-is-no-sequence-associated-with-table-table","errorCode":null,"errorMessage":"There is no sequence associated with table '$tableName'.","messagePattern":"There is no sequence associated with table '\\$tableName'\\.","errorType":"exception","errorClass":"yii\\base\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"framework/db/mysql/QueryBuilder.php","lineNumber":180,"sourceCode":"     */\n    public function resetSequence($tableName, $value = null)\n    {\n        $table = $this->db->getTableSchema($tableName);\n        if ($table !== null && $table->sequenceName !== null) {\n            $tableName = $this->db->quoteTableName($tableName);\n            if ($value === null) {\n                $key = reset($table->primaryKey);\n                $value = $this->db->createCommand(\"SELECT MAX(`$key`) FROM $tableName\")->queryScalar() + 1;\n            } else {\n                $value = (int) $value;\n            }\n\n            return \"ALTER TABLE $tableName AUTO_INCREMENT=$value\";\n        } elseif ($table === null) {\n            throw new InvalidArgumentException(\"Table not found: $tableName\");\n        }\n\n        throw new InvalidArgumentException(\"There is no 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. Meaningless for MySQL.\n     * @param string $table the table name. Meaningless for MySQL.\n     * @return string the SQL statement for checking integrity\n     */\n    public function checkIntegrity($check = true, $schema = '', $table = '')\n    {\n        return 'SET FOREIGN_KEY_CHECKS = ' . ($check ? 1 : 0);\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function buildLimit($limit, $offset)","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mysql/QueryBuilder.php#L162-L198","documentation":"MySQL has no standalone sequence objects; yii\\db\\mysql\\QueryBuilder::resetSequence() maps sequences onto AUTO_INCREMENT columns via TableSchema::sequenceName. This exception fires when the table schema WAS found but ->sequenceName is null, i.e. the table has no AUTO_INCREMENT column, so there is nothing to reset.","triggerScenarios":"Calling resetSequence() on a table whose primary key is not AUTO_INCREMENT: a CHAR(36)/BINARY(16) UUID key, an app-assigned integer key, a composite key, or a table with no primary key at all.","commonSituations":"Fixture/base classes that blindly reset every table's sequence in projects using UUID or manually assigned primary keys; switching a table's PK strategy from auto-increment to application-generated IDs without updating fixture code.","solutions":["Only call resetSequence() on tables whose primary key is an AUTO_INCREMENT column.","If IDs are assigned by the application, remove the resetSequence() call - there is no counter to reset.","If auto numbering is intended, alter the table so the PK column is AUTO_INCREMENT.","Guard the call: check $db->getTableSchema($t)->sequenceName !== null first."],"exampleFix":"// before\n$db->createCommand()->resetSequence('order')->execute(); // 'order' uses UUID PKs\n\n// after\n$schema = $db->getTableSchema('order', true);\nif ($schema !== null && $schema->sequenceName !== null) {\n    $db->createCommand()->resetSequence('order')->execute();\n}","handlingStrategy":"validation","validationCode":"$schema = $db->getTableSchema($table, true);\nif ($schema === null || $schema->sequenceName === null) {\n    // no AUTO_INCREMENT column: nothing to reset\n    return;\n}\n$db->createCommand()->resetSequence($table)->execute();","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->resetSequence($table)->execute();\n} catch (\\yii\\base\\InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'no sequence')) {\n        // table has no AUTO_INCREMENT column; safe to skip\n    }\n}","preventionTips":["Maintain a whitelist of auto-increment tables for fixture resets instead of iterating all tables.","Document which entities use UUID/application-assigned keys so reset logic can skip them.","Assert sequenceName in test setup rather than during the reset call."],"tags":["yii2","php","mysql","database","sequence","auto-increment","primary-key"],"backgroundTag":"missing-database-sequence","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}