{"record":{"id":"615731cccab62ded","repo":"yiisoft/yii2","slug":"table-not-found-tablename-615731","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/mysql/QueryBuilder.php","lineNumber":177,"sourceCode":"     * the next new row's primary key will have a value 1.\n     * @return string the SQL statement for resetting sequence\n     * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table.\n     */\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    /**","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mysql/QueryBuilder.php#L159-L195","documentation":"Thrown by yii\\db\\mysql\\QueryBuilder::resetSequence() when $this->db->getTableSchema($tableName) returns null, meaning MySQL schema introspection cannot see the table in the database the connection points to. The method needs a real table schema to read the primary key and emit an ALTER TABLE ... AUTO_INCREMENT statement. It is normally reached through yii\\db\\Command::resetSequence()->execute() or Command::executeResetSequence().","triggerScenarios":"Calling $db->createCommand()->resetSequence('tbl')->execute() (or QueryBuilder::resetSequence() directly) with a table that does not exist, is misspelled, lives in a different database than the DSN selects, or was dropped/renamed while a stale schema cache keeps returning null.","commonSituations":"Fixture or test code resetting AUTO_INCREMENT counters against a test database that was never migrated; enableSchemaCache serving a stale table list after DDL changes; console commands wired to a different environment than the web app; wrong database name in the DSN.","solutions":["Verify the table is visible to this connection: $db->getTableSchema($table, true) with refresh=true, or SHOW TABLES on the same DSN.","If schema caching is enabled, refresh it: $db->schema->refresh() or flush the application cache component holding the schema cache.","Check the table name spelling and apply the table prefix manually if you bypass Yii's {{%table}} expansion.","Make sure migrations creating the table have run in this environment before resetting its sequence."],"exampleFix":"// before\n$db->createCommand()->resetSequence('post')->execute();\n\n// after\nif ($db->getTableSchema('post', true) === null) {\n    throw new InvalidArgumentException(\"Cannot reset sequence: table 'post' not found in this database.\");\n}\n$db->createCommand()->resetSequence('post')->execute();","handlingStrategy":"validation","validationCode":"// Before resetSequence(): confirm the table is visible\nif ($db->getTableSchema($tableName, true) === null) {\n    throw new InvalidArgumentException(\"Table '$tableName' not found; check DSN and run migrations.\");\n}\n$db->createCommand()->resetSequence($tableName)->execute();","typeGuard":"use yii\\db\\TableSchema;\n\nfunction tableSchemaOrFail(?TableSchema $schema, string $name): TableSchema\n{\n    if ($schema === null) {\n        throw new InvalidArgumentException(\"Table not found: $name\");\n    }\n    return $schema;\n}","tryCatchPattern":"try {\n    $db->createCommand()->resetSequence($table)->execute();\n} catch (\\yii\\base\\InvalidArgumentException $e) {\n    // table vanished (renamed/dropped concurrently): refresh schema and re-check\n    $db->schema->refresh();\n    if ($db->getTableSchema($table, true) === null) {\n        // handle missing table\n    }\n}","preventionTips":["Refresh or disable schema cache in long-running workers after DDL changes.","Reset sequences only after confirming migrations ran in the target environment.","Wrap fixture-level sequence resets in a shared helper that validates the schema first."],"tags":["yii2","php","mysql","database","sequence","auto-increment","schema-cache"],"backgroundTag":"database-table-not-found","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}