yiisoft/yii2 · error · yii\base\InvalidArgumentException
Table not found: $tableName
Error message
Table not found: $tableName
What it means
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().
Source
Thrown at framework/db/mysql/QueryBuilder.php:177
* the next new row's primary key will have a value 1.
* @return string the SQL statement for resetting sequence
* @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table.
*/
public function resetSequence($tableName, $value = null)
{
$table = $this->db->getTableSchema($tableName);
if ($table !== null && $table->sequenceName !== null) {
$tableName = $this->db->quoteTableName($tableName);
if ($value === null) {
$key = reset($table->primaryKey);
$value = $this->db->createCommand("SELECT MAX(`$key`) FROM $tableName")->queryScalar() + 1;
} else {
$value = (int) $value;
}
return "ALTER TABLE $tableName AUTO_INCREMENT=$value";
} elseif ($table === null) {
throw new InvalidArgumentException("Table not found: $tableName");
}
throw new InvalidArgumentException("There is no sequence associated with table '$tableName'.");
}
/**
* Builds a SQL statement for enabling or disabling integrity check.
* @param bool $check whether to turn on or off the integrity check.
* @param string $schema the schema of the tables. Meaningless for MySQL.
* @param string $table the table name. Meaningless for MySQL.
* @return string the SQL statement for checking integrity
*/
public function checkIntegrity($check = true, $schema = '', $table = '')
{
return 'SET FOREIGN_KEY_CHECKS = ' . ($check ? 1 : 0);
}
/**View on GitHub (pinned to 66f00d18a2)
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.
Example fix
// before
$db->createCommand()->resetSequence('post')->execute();
// after
if ($db->getTableSchema('post', true) === null) {
throw new InvalidArgumentException("Cannot reset sequence: table 'post' not found in this database.");
}
$db->createCommand()->resetSequence('post')->execute(); Defensive patterns
Strategy: validation
Validate before calling
// Before resetSequence(): confirm the table is visible
if ($db->getTableSchema($tableName, true) === null) {
throw new InvalidArgumentException("Table '$tableName' not found; check DSN and run migrations.");
}
$db->createCommand()->resetSequence($tableName)->execute(); Type guard
use yii\db\TableSchema;
function tableSchemaOrFail(?TableSchema $schema, string $name): TableSchema
{
if ($schema === null) {
throw new InvalidArgumentException("Table not found: $name");
}
return $schema;
} Try / catch
try {
$db->createCommand()->resetSequence($table)->execute();
} catch (\yii\base\InvalidArgumentException $e) {
// table vanished (renamed/dropped concurrently): refresh schema and re-check
$db->schema->refresh();
if ($db->getTableSchema($table, true) === null) {
// handle missing table
}
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- There is no sequence associated with table '$tableName'.
- Unable to find column '$column' in table '$table'.
- MySQL < 8.0.16 does not support check constraints.
- MySQL does not support default value constraints.
- Unknown table: $table
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/615731cccab62ded.
Report an issue: GitHub.