Skip to content Skip to sidebar Skip to footer

Duplicate Key Value Violates Unique Constraint, Cakephp

I have set my postgres database populated and all constraints applied. I ran cake bake all on all tables and start to test. I got this error: Error: SQLSTATE[23505]: Unique viola

Solution 1:

A couple of things:

  • I don't see a key 7, or 545 in the SQL statement; so where do these values come from?
  • The created column gets filled with 'now()' as a string, not a value from the NOW() function. Fill it with the proper datetime in your PHP code (date('Y-m-d H:i:s') usually does the trick).

Anyway, if you are inserting records in a loop, or just more than one with the same model, don't forget to call create() on the model otherwise the ID will be the same.

Post a Comment for "Duplicate Key Value Violates Unique Constraint, Cakephp"