1

I am running a script in Symfony2/Doctrine2 project.

And I receive the Doctrine\DBAL\Exception\UniqueConstraintViolationException exception.

An exception occurred while executing 
'INSERT INTO user (name, created_at) VALUES (?, ?)' 
with params ["Gaurav" "2016-07-31 17:10:54"]:               

SQLSTATE[23000]: Integrity constraint violation: 
1062 Duplicate entry 'Gaurav' for key 'UNIQ_8C9F36105E237E06'

Any idea, how I can avoid this?

I have been using Repository findByOne() and then updating the record. It still generates an SQL query.

I found the following post. But I am not sure, if this is necessary.

Please advise.

Community
  • 1
  • 1
Gaurav
  • 191
  • 18
  • How are you doing your 'updates'? Getting an INSERT from and update seems strange. I suspect a batch operation of some sort. – Cerad Jul 31 '16 at 11:42
  • @Cerad Yes. Its a kinda batch operation, as its a loop reading input from file. – Gaurav Jul 31 '16 at 15:32

1 Answers1

0

Check how did you define db schema. This exception tells you that you tried to insert duplicate value for column which is defined as unique.

  • Yes. But it should not reach the db layer if I get the record from repository and try to update the managed entity. – Gaurav Jul 31 '16 at 15:30