One-to-one relation resulting in two identically named indexes #6372

Open
opened 2026-01-22 15:31:57 +01:00 by admin · 0 comments
Owner

Originally created by @melvinkoopmans on GitHub (Dec 20, 2019).

Q A
Version 2.5

Support Question

I have two entities ContentItemVote and Comment which both have a One-to-One relationship with the ContentIndex entity. The content index has a field itemType (comment or content-item) and itemID (the ID of the comment or content item).

It looks like this:

    <one-to-one field="contentIndex" target-entity="App\Module\ContentIndex\Entity\ContentIndex" fetch="LAZY">
      <join-columns>
        <join-column name="ItemID" referenced-column-name="ItemID"/>
        <join-column name="ItemType" referenced-column-name="ItemType"/>
      </join-columns>
    </one-to-one>

Doctrine will create a unique index with these columns. The name of this index is based on the field name (contentIndex). This results in two indexes with the same name, which is not possible.

In Doctrine/ORM/Mapping/ClassMetadataInfo.php', line 1594:

if ($mapping['type'] === self::ONE_TO_ONE && ! $this->isInheritanceTypeSingleTable()) {
    if (count($mapping['joinColumns']) === 1) {
        if (empty($mapping['id'])) {
            $joinColumn['unique'] = true;
        }
    } else {
        $uniqueConstraintColumns[] = $joinColumn['name'];
    }
}

and then later at line 1631:

$this->table['uniqueConstraints'][$mapping['fieldName'] . "_uniq"] = [
    'columns' => $uniqueConstraintColumns
];

Now both indexes are named contentIndex_uniq and I get a SQL error: SQLSTATE[HY000]: General error: 1 index contentIndex_uniq already exists.

Is this a bug in Doctrine? Am I doing something wrong? It would be helpful if I could chose the name of this index myself.

Thanks in advance!

Originally created by @melvinkoopmans on GitHub (Dec 20, 2019). <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ----- | Version | 2.5 ### Support Question I have two entities `ContentItemVote` and `Comment` which both have a One-to-One relationship with the `ContentIndex` entity. The content index has a field `itemType` (`comment` or `content-item`) and `itemID` (the ID of the comment or content item). It looks like this: ```xml <one-to-one field="contentIndex" target-entity="App\Module\ContentIndex\Entity\ContentIndex" fetch="LAZY"> <join-columns> <join-column name="ItemID" referenced-column-name="ItemID"/> <join-column name="ItemType" referenced-column-name="ItemType"/> </join-columns> </one-to-one> ``` Doctrine will create a unique index with these columns. The name of this index is based on the field name (`contentIndex`). This results in two indexes with the same name, which is not possible. In `Doctrine/ORM/Mapping/ClassMetadataInfo.php'`, line `1594`: ```php if ($mapping['type'] === self::ONE_TO_ONE && ! $this->isInheritanceTypeSingleTable()) { if (count($mapping['joinColumns']) === 1) { if (empty($mapping['id'])) { $joinColumn['unique'] = true; } } else { $uniqueConstraintColumns[] = $joinColumn['name']; } } ``` and then later at line `1631`: ```php $this->table['uniqueConstraints'][$mapping['fieldName'] . "_uniq"] = [ 'columns' => $uniqueConstraintColumns ]; ``` Now both indexes are named `contentIndex_uniq` and I get a SQL error: `SQLSTATE[HY000]: General error: 1 index contentIndex_uniq already exists`. Is this a bug in Doctrine? Am I doing something wrong? It would be helpful if I could chose the name of this index myself. Thanks in advance!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6372