cakephp/cakephp · error · DatabaseException
Table ` ` does not contain a index named ` `.
Error message
Table `%s` does not contain a index named `%s`.
What it means
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.
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.
Example fix
// before
$idx = $schema->index('author_idx');
// after
$idx = $schema->hasIndex('author_idx') ? $schema->index('author_idx') : null; Defensive patterns
Strategy: type-guard
Validate before calling
if (!in_array($name, $schema->indexes(), true)) {
return null;
} Type guard
function indexOrNullable(\Cake\Database\Schema\TableSchema $s, string $name): ?array {
return in_array($name, $s->indexes(), true) ? $s->index($name) : null;
} Try / catch
try {
$idx = $schema->index($name);
} catch (\Cake\Database\Exception\DatabaseException $e) {
$idx = null;
} Prevention
- 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
When it happens
Trigger: `$schema->index('idx_name')` where addIndex() was never called with that name, or the name is misspelled; retrieving an index removed by dropIndex().
Common situations: Inspection code assuming an index exists; typos in index names; reading a schema object that was rebuilt without the index.
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
- Column ` ` of table ` `: The column type ` ` can only be…
- Columns used in constraints must be added to the Table…
- Columns used in index
- Constraints in table
- Invalid constraint type
AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12).
Data as JSON: /api/errors/fd2595f980f1ad68.
Report an issue: GitHub.
Appendix: source
Thrown at src/Database/Schema/TableSchema.php:668
/**
* Get a index object for a given index name.
*
* Will raise an exception if no index can be found.
*
* @param string $name The name of the index to get.
* @return \Cake\Database\Schema\Index
*/
public function index(string $name): Index
{
$index = $this->_indexes[$name] ?? null;
if ($index === null) {
$message = sprintf(
'Table `%s` does not contain a index named `%s`.',
$this->_table,
$name,
);
throw new DatabaseException($message);
}
return $index;
}
/**
* @inheritDoc
*/
public function getPrimaryKey(): array
{
foreach ($this->_constraints as $data) {
if ($data->getType() === static::CONSTRAINT_PRIMARY) {
return (array)$data->getColumns();
}
}
return [];
}View on GitHub (pinned to 1128eba9b0)