Unnamed indexes from the parent class are ignored if there are unnamed indexes in the subclass
Bug Report
| Q | A |
|---|---|
| Version | 3.5.2 |
Summary
Unnamed indexes from the parent class are ignored if there are unnamed indexes in the subclass when building the schema from metadata.
Current behavior
Indexes are ignored.
Expected behavior
All indexes from the parent class are added to the subclass, regardless of their naming.
How to reproduce
Parent entity with unnamed indexes to ensure index names' uniqueness in the subclass.
#[ORM\Index(columns: ['created_by'])]
#[ORM\Index(columns: ['created_at'])]
#[ORM\Index(columns: ['updated_at'])]
#[ORM\Index(columns: ['updated_by'])]
#[ORM\MappedSuperclass] # Force creation of indexes in child entities
class BaseEntity
{
public function __construct(
#[ORM\Column(length: 40, nullable: true)]
public ?string $createdBy = null,
#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE, nullable: true)]
public ?\DateTimeImmutable $createdAt = null,
#[ORM\Column(length: 40, nullable: true)]
public ?string $updatedBy = null,
#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE, nullable: true)]
public ?\DateTimeImmutable $updatedAt = null,
) {
}
}In PostgerSQL, all index names must be unique across the scheme. If I set index names here, Doctrine will try to create multiple indexes with the same name, leading to a broken schema. To avoid this, I leave indexes unnamed to make Doctrine generate unique IDs as names to ensure uniqueness.
Now I make subclass entities like this:
#[ORM\Index(columns: ['number', 'linked'])]
#[ORM\Index(columns: ['linked'])]
#[ORM\ChangeTrackingPolicy('DEFERRED_EXPLICIT')]
class Foo extends BaseEntity
{
public function __construct(
#[ORM\Id]
#[ORM\Column(type: MyIdType::NAME, length: MyIdType::FIELD_LENGTH, unique: true)]
public NumberPlateId $id,
#[ORM\Column(type: Types::STRING, length: NumberPlateFieldLength::NUMBER)]
public string $number,
#[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])]
public bool $linked = false,
# Parent entity
?string $createdBy = null,
?DateTimeImmutable $createdAt = null,
?string $updatedBy = null,
?DateTimeImmutable $updatedAt = null,
) {
parent::__construct(
createdBy: $createdBy,
createdAt: $createdAt,
updatedBy: $updatedBy,
updatedAt: $updatedAt,
);
}
}I expect that schema generated from metadata for this table will have 6 simple indexes:
- createdBy
- createdAt
- updatedBy
- updatedAt
- number, linked
- linked
But in the reality, I get only four indexes:
- number, linked
- linked
- updatedBy
- updatedAt
Because of this, proper indexes are never created in the database. The generated metadata does not represent the intended schema properly.
The issue is in two places:
- Unnamed indexes - stored with numeric keys in the class metadata.
- Method
\Doctrine\ORM\Mapping\ClassMetadataFactory::addInheritedIndexes()that adds parent class indexes to the subclass. It effectively ignores field names and skips adding indexes if the same key already exists.
Thus, when the addInheritedIndexes() method loops over four numeric keys of the BaseEntity class in my example, it checks for numeric keys in the Foo class. But Foo already has two unnamed indexes stored with numeric keys as well! So addInheritedIndexes() wrongly skips the first two indexes from BaseEntity.
private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass): void
{
if (! $parentClass->isMappedSuperclass) {
return;
}
foreach (['uniqueConstraints', 'indexes'] as $indexType) {
if (isset($parentClass->table[$indexType])) {
foreach ($parentClass->table[$indexType] as $indexName => $index) {
if (isset($subClass->table[$indexType][$indexName])) { // <------------------ THE ERROR IS HERE
continue; // Let the inheriting table override indices
}
$subClass->table[$indexType][$indexName] = $index;
}
}
}
}It looks like this method is completely unaware of unnamed indexes and expects only named ones! It should compare unnamed indexes with numeric keys by their fields, not just the key in the table[$indexType] class field.
Source: doctrine/orm