DDC-1359: Deleting and inserting with unique index #1977

doctrinebot opened this Issue Sep 2, 2011 · 3 comments

2 participants


Jira issue originally created by user rrafal@gmail.com:

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 userinfo tables. userinfo stores additional data about a user as a list of key-values.
user: id, name, email, password
userinfo: id, user_id, info_key, infovalue

I created a unique index on userinfo table: CREATE UNIQUE INDEX user_info_key ON user_info (`userid,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 created by @ocramius:

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...


Issue was closed with resolution "Invalid"


Comment created by @ocramius:

Invalid usage of collection API/object graph

@Ocramius Ocramius was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment