[DDC-1359] Deleting and inserting with unique index Created: 02/Sep/11  Updated: 09/Feb/13  Resolved: 09/Feb/13

Status: Closed
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.0.7
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Rafal Radulski Assignee: Marco Pivetta
Resolution: Invalid Votes: 1
Labels: None



I'm getting a database error when deleting and inserting records in a single unit of work. Consider this example:

I have a user and user_info tables. user_info stores additional data about a user as a list of key-values.
user: id, name, email, password
user_info: id, user_id, info_key, info_value

I created a unique index on user_info table: CREATE UNIQUE INDEX `user_info_key` ON `user_info` (`user_id` , `key` ) ;

Each table is represented as an Entity class.

Now, I run this code:
1. $user->removeAllInfo(); // remove old entries
2. $user->addInfo('first_name', 'John'); // add new entries
3. $this->em->flush(); // save

This causes MySQL error: "Integrity constraint violation: 1062 Duplicate entry". Doctrine ORM first persists new entities and then deletes old entities. I think the relevant code is at Doctrine/ORM/UnitOfWork :277-313. Everything works fine if I add flush() before line 2. Also, everything works fine if I change the index to non-unique.

Comment by Marco Pivetta [ 08/Jul/12 ]

I just got back to the UnitOfWork and (in latest master) it executes deletes as last operation (maybe to avoid cascades), and this is also documented.

In my opinion, you should do the operation of computing the changeset on your `info` collection manually...

Comment by Marco Pivetta [ 09/Feb/13 ]

Invalid usage of collection API/object graph

Generated at Tue Dec 01 01:04:56 EST 2015 using JIRA 6.4.10#64025-sha1:5b8b74079161cd76a20ab66dda52747ee6701bd6.