yiisoft/yii2 · error · InvalidArgumentException
Table not found: $tableName
Error message
Table not found: $tableName
What it means
yii\db\pgsql\QueryBuilder::resetSequence() emits SELECT SETVAL(...) to reseed a PostgreSQL sequence and needs the table schema to find the primary key. When $this->db->getTableSchema($tableName) returns null it throws InvalidArgumentException - the table is not visible to the connected database/schema, often due to search_path, schema-qualification, or cache staleness.
Source
Thrown at framework/db/pgsql/QueryBuilder.php:196
* @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) {
// c.f. https://www.postgresql.org/docs/8.1/functions-sequence.html
$sequence = $this->db->quoteTableName($table->sequenceName);
$tableName = $this->db->quoteTableName($tableName);
if ($value === null) {
$key = $this->db->quoteColumnName(reset($table->primaryKey));
$value = "(SELECT COALESCE(MAX({$key}),0) FROM {$tableName})+1";
} else {
$value = (int) $value;
}
return "SELECT SETVAL('$sequence',$value,false)";
} elseif ($table === null) {
throw new InvalidArgumentException("Table not found: $tableName");
}
throw new InvalidArgumentException("There is not 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.
* @param string $table the table name.
* @return string the SQL statement for checking integrity
*/
public function checkIntegrity($check = true, $schema = '', $table = '')
{
/** @var Schema $dbSchema */
$dbSchema = $this->db->getSchema();
$enable = $check ? 'ENABLE' : 'DISABLE';
$schema = $schema ?: $dbSchema->defaultSchema;View on GitHub (pinned to 66f00d18a2)
Solutions
- Verify with $db->getTableSchema($table, true) before resetting.
- Use the schema-qualified name ('schema.table') if the table lives outside search_path.
- Refresh the schema: $db->schema->refresh() or flush the cache component.
- Confirm migrations ran in the target environment.
Example fix
// before
$db->createCommand()->resetSequence('event')->execute();
// after
$table = 'audit.event';
if ($db->getTableSchema($table, true) === null) {
throw new InvalidArgumentException("Table not found: $table");
}
$db->createCommand()->resetSequence($table)->execute(); Defensive patterns
Strategy: validation
Validate before calling
// Use schema-qualified name and refresh introspection
if ($db->getTableSchema($table, true) === null) {
throw new InvalidArgumentException("Table not found: $table");
}
$db->createCommand()->resetSequence($table)->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) {
$db->schema->refresh();
if ($db->getTableSchema($table, true) === null) { /* handle */ }
} Prevention
- Set search_path explicitly in the DSN or use schema-qualified names everywhere.
- Invalidate schema cache after DDL in the same process.
- Assert fixture tables exist before resetting sequences.
When it happens
Trigger: Calling $db->createCommand()->resetSequence('tbl')->execute() where 'tbl' is misspelled, exists in a non-public schema not on search_path, was dropped/renamed, or the schema cache holds a stale miss.
Common situations: Multi-schema PostgreSQL apps where the table is 'audit.event' but search_path only has public; test databases not migrated; schema cache not invalidated after DDL; wrong database in the DSN.
Related errors
- There is not sequence associated with table '$tableName'.
- PostgreSQL does not support default value constraints.
- Table not found: $tableName
- There is no sequence associated with table '$tableName'.
- Unknown table: $table
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/13233f78c1bad27f.
Report an issue: GitHub.