One-To-One relationships with missing unique index causes invalid drop index on database diff #6922

Closed
opened 2026-01-22 15:41:24 +01:00 by admin · 2 comments
Owner

Originally created by @jackbentley on GitHub (Jan 31, 2022).

👉 Originally reported at https://github.com/doctrine/dbal/issues/5210

Bug Report

Summary

Bug was experienced while using PostgreSQL.

One-to-one relationships are expected to have a unique index.
https://github.com/doctrine/orm/blob/2.11.x/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L1893

When running doctrine:schema:update, ORM will create an expected table schema with a unique index named UNIQ_.
https://github.com/doctrine/orm/blob/2.11.x/lib/Doctrine/ORM/Tools/SchemaTool.php#L745

When generating a table object from current schema, an index is always added for all FKs which is non-unique and with the prefix IDX_. This isn't correct for One-To-One relations which will have a unique index prefixed with UNIQ_.
https://github.com/doctrine/dbal/blob/3.3.x/src/Schema/Table.php#L578-L592

In my particular case, the table existed, a FK was present for the relation, but an index was not.

Current behaviour

This causes a "phantom" drop index. The code thinks the index exists in the database when it does not. The index is not present in the expected schema and is told to be dropped.

     DROP INDEX IDX_A0BF0B2B40C46575;
     CREATE UNIQUE INDEX UNIQ_A0BF0B2B40C46575 ON foo (bar_id);

Expected behaviour

The drop index shouldn't be outputted

     CREATE UNIQUE INDEX UNIQ_A0BF0B2B40C46575 ON foo (bar_id);

How to reproduce

  1. Create an two entities with a One-To-One relationship between each other
  2. Create both tables / Run doctrine:schema:create
  3. Remove the unique index on the owning side of the relationship. Keep the FK.
  4. Run doctrine:schema:update
Originally created by @jackbentley on GitHub (Jan 31, 2022). > 👉 Originally reported at https://github.com/doctrine/dbal/issues/5210 ### Bug Report #### Summary Bug was experienced while using PostgreSQL. One-to-one relationships are expected to have a unique index. https://github.com/doctrine/orm/blob/2.11.x/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L1893 When running `doctrine:schema:update`, ORM will create an expected table schema with a unique index named `UNIQ_`. https://github.com/doctrine/orm/blob/2.11.x/lib/Doctrine/ORM/Tools/SchemaTool.php#L745 When generating a table object from current schema, an index is always added for all FKs which is non-unique and with the prefix `IDX_`. This isn't correct for One-To-One relations which will have a unique index prefixed with `UNIQ_`. https://github.com/doctrine/dbal/blob/3.3.x/src/Schema/Table.php#L578-L592 In my particular case, the table existed, a FK was present for the relation, but an index was not. #### Current behaviour This causes a "phantom" drop index. The code thinks the index exists in the database when it does not. The index is not present in the expected schema and is told to be dropped. ```sql DROP INDEX IDX_A0BF0B2B40C46575; CREATE UNIQUE INDEX UNIQ_A0BF0B2B40C46575 ON foo (bar_id); ``` #### Expected behaviour The drop index shouldn't be outputted ```sql CREATE UNIQUE INDEX UNIQ_A0BF0B2B40C46575 ON foo (bar_id); ``` #### How to reproduce 1. Create an two entities with a One-To-One relationship between each other 2. Create both tables / Run `doctrine:schema:create` 3. Remove the unique index on the owning side of the relationship. Keep the FK. 4. Run `doctrine:schema:update`
admin closed this issue 2026-01-22 15:41:25 +01:00
Author
Owner

@beberlei commented on GitHub (Feb 2, 2022):

Just use Many to One if you don't want the unique index. That is the semantical difference between them.

@beberlei commented on GitHub (Feb 2, 2022): Just use Many to One if you don't want the unique index. That is the semantical difference between them.
Author
Owner

@jackbentley commented on GitHub (Feb 2, 2022):

@beberlei I think you might've misunderstood. I want the unique index. It's expected. The schema update is trying to delete a non-unique index that doesn't exist before adding the expected unique index.

@jackbentley commented on GitHub (Feb 2, 2022): @beberlei I think you might've misunderstood. I want the unique index. It's expected. The schema update is trying to delete a non-unique index that doesn't exist before adding the expected unique index.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6922