Issue Task Resolved

#110256Do not rely on Doctrine schema object array keys

Stefan Bürk 07/21/26, 19:24 updated 07/23/26, 12:40

Description

`Doctrine\DBAL\Schema\Table` keys its columns, indexes and foreign key
constraints by a normalized name of its own – `strtolower()` plus trimmed
identifier quotes. Two places rebuild a `Table` and rely on those keys to
deduplicate the passed definitions:

1) `TYPO3\CMS\Core\Database\Schema\Parser\TableBuilder::addIndex()`

array_merge($this->table->getIndexes(), [strtolower($indexName) => $index])
The key is built by the caller with `strtolower()` only, while `$indexName`
originates from `getQuotedName()`. For a quoted index name the key therefore
is `` `myidx` `` and never matches Doctrine’s `myidx`, so re-defining that
index does not replace the previous one. The duplicate is then passed on to
`Table::_addIndex()`.

2) `TYPO3\CMS\Core\Database\Schema\SchemaMigrator::mergeTableDefinitions()`

array_merge($currentTableDefinition->getForeignKeys(), $table->getForeignKeys())
Deduplication happens by the Doctrine array key only. The neighbouring
columns and indexes are already merged explicitly by
`mergeColumns()` / `mergeIndexes()`, the foreign keys are the odd one out.

Both spots work today only because the keys happen to line up. `Table::__construct()`
re-derives every key itself through `_addIndex()` / `_addForeignKeyConstraint()`,
so the passed keys are discarded anyway – they only ever mattered for the
`array_merge()` deduplication. Depending on an internal keying scheme of another
library for correctness is fragile, and Doctrine DBAL 5.0 reworks exactly this area.

Both places are changed to merge explicitly by the object’s own name, mirroring
the existing `mergeColumns()` / `mergeIndexes()` pattern. That also fixes the
quoted index name case described above.

One detail to be aware of for foreign keys: for an unnamed constraint Doctrine
generates a name for the array key only, it is never set on the constraint
itself, so `getName()` returns an empty string. Merging those by name would
collapse all unnamed constraints into a single one, therefore they are kept as
they are. This cannot be triggered through the public API today, since
`Table::addForeignKeyConstraint()` always assigns a generated name, but the
merge should not depend on that.

Reviews 3