mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-3354: Replacing indexed item on association with indexBy cannot comply with unicity constraint #4147
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Oct 17, 2014).
Originally assigned to: @beberlei on GitHub.
Jira issue originally created by user cblegare:
Using a bidirectional one-to-many relation having an 'indexBy' clause on the owning side and a unique constraint over the indexed field and the 'mappedBy' field of the inverse side, replaced indexed fields being inserted before removed violated the unique constraint.
As stated in the code examples below, if done without the unique constraint, when replacing an indexed field, the row is replaced in the database (new one created, old one deleted).
Here is an example:
IMO, Using 'indexBy' on a one-to-many relation should automatically generate a unique constraint for the combination of the indexed field and the 'mappedBy' field.
Documentation states that
{quote}
Fields that are used for the index by feature HAVE to be unique in the database. The behavior for multiple entities with the same index-by field value is undefined.
{quote}
which is unclear (especially the use of the word 'database'). I wonder why indexes used for association should be unique for a whole table.
@doctrinebot commented on GitHub (Oct 19, 2014):
Comment created by @ocramius:
{quote}I wonder why indexes used for association should be unique for a whole table.{quote}
That's because we can't ensure that indexes aren't overwritten when hydrating a collection: that is what the behavior "undefined" stands for.
As for replacing values in the collection, that's how the ORM works in any case, as it inserts data before removing any data to avoid removes from causing FK constraint failures (see
d361ed904e/lib/Doctrine/ORM/UnitOfWork.php (L347-L379))