{"record":{"id":"fd2595f980f1ad68","repo":"cakephp/cakephp","slug":"table-s-does-not-contain-a-index-named-s","errorCode":null,"errorMessage":"Table `%s` does not contain a index named `%s`.","messagePattern":"Table `(.+?)` does not contain a index named `(.+?)`\\.","errorType":"exception","errorClass":"DatabaseException","httpStatus":null,"severity":"error","filePath":"src/Database/Schema/TableSchema.php","lineNumber":668,"sourceCode":"\n    /**\n     * Get a index object for a given index name.\n     *\n     * Will raise an exception if no index can be found.\n     *\n     * @param string $name The name of the index to get.\n     * @return \\Cake\\Database\\Schema\\Index\n     */\n    public function index(string $name): Index\n    {\n        $index = $this->_indexes[$name] ?? null;\n        if ($index === null) {\n            $message = sprintf(\n                'Table `%s` does not contain a index named `%s`.',\n                $this->_table,\n                $name,\n            );\n            throw new DatabaseException($message);\n        }\n\n        return $index;\n    }\n\n    /**\n     * @inheritDoc\n     */\n    public function getPrimaryKey(): array\n    {\n        foreach ($this->_constraints as $data) {\n            if ($data->getType() === static::CONSTRAINT_PRIMARY) {\n                return (array)$data->getColumns();\n            }\n        }\n\n        return [];\n    }","sourceCodeStart":650,"sourceCodeEnd":686,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/Database/Schema/TableSchema.php#L650-L686","documentation":"TableSchema::index() performs a lookup of a declared index by name and throws when no index with that name exists on the table. Unlike hasIndex(), it treats a missing name as a hard error rather than returning null.","triggerScenarios":"`$schema->index('idx_name')` where addIndex() was never called with that name, or the name is misspelled; retrieving an index removed by dropIndex().","commonSituations":"Inspection code assuming an index exists; typos in index names; reading a schema object that was rebuilt without the index.","solutions":["Check with `$schema->hasIndex('name')` (or compare against $schema->indexes()) first.","Verify the exact index name used in addIndex().","Re-add the index if it was dropped earlier in the flow.","Return an empty/null result defensively instead of calling index() for optional lookups."],"exampleFix":"// before\n$idx = $schema->index('author_idx');\n// after\n$idx = $schema->hasIndex('author_idx') ? $schema->index('author_idx') : null;","handlingStrategy":"type-guard","validationCode":"if (!in_array($name, $schema->indexes(), true)) {\n    return null;\n}","typeGuard":"function indexOrNullable(\\Cake\\Database\\Schema\\TableSchema $s, string $name): ?array {\n    return in_array($name, $s->indexes(), true) ? $s->index($name) : null;\n}","tryCatchPattern":"try {\n    $idx = $schema->index($name);\n} catch (\\Cake\\Database\\Exception\\DatabaseException $e) {\n    $idx = null;\n}","preventionTips":["Gate index() with hasIndex() or indexes()","Reuse the same name constants for addIndex/index","Check dropIndex() calls earlier in the flow","Use hasIndex('name', ['type' => ...]) variants when checking specific forms"],"tags":["database","schema","cakephp"],"backgroundTag":"record-not-found","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}