{"record":{"id":"13233f78c1bad27f","repo":"yiisoft/yii2","slug":"table-not-found-tablename-13233f","errorCode":null,"errorMessage":"Table not found: $tableName","messagePattern":"Table not found: \\$tableName","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"framework/db/pgsql/QueryBuilder.php","lineNumber":196,"sourceCode":"     * @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            // c.f. https://www.postgresql.org/docs/8.1/functions-sequence.html\n            $sequence = $this->db->quoteTableName($table->sequenceName);\n            $tableName = $this->db->quoteTableName($tableName);\n            if ($value === null) {\n                $key = $this->db->quoteColumnName(reset($table->primaryKey));\n                $value = \"(SELECT COALESCE(MAX({$key}),0) FROM {$tableName})+1\";\n            } else {\n                $value = (int) $value;\n            }\n\n            return \"SELECT SETVAL('$sequence',$value,false)\";\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 ? 'ENABLE' : 'DISABLE';\n        $schema = $schema ?: $dbSchema->defaultSchema;","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/pgsql/QueryBuilder.php#L178-L214","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$db->createCommand()->resetSequence('event')->execute();\n\n// after\n$table = 'audit.event';\nif ($db->getTableSchema($table, true) === null) {\n    throw new InvalidArgumentException(\"Table not found: $table\");\n}\n$db->createCommand()->resetSequence($table)->execute();","handlingStrategy":"validation","validationCode":"// Use schema-qualified name and refresh introspection\nif ($db->getTableSchema($table, true) === null) {\n    throw new InvalidArgumentException(\"Table not found: $table\");\n}\n$db->createCommand()->resetSequence($table)->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    $db->schema->refresh();\n    if ($db->getTableSchema($table, true) === null) { /* handle */ }\n}","preventionTips":["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."],"tags":["yii2","php","postgresql","pgsql","database","sequence","search-path"],"backgroundTag":"database-table-not-found","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}