{"action":"created","issue":{"url":"https://api.github.com/repos/propelorm/Propel2/issues/1180","repository_url":"https://api.github.com/repos/propelorm/Propel2","labels_url":"https://api.github.com/repos/propelorm/Propel2/issues/1180/labels{/name}","comments_url":"https://api.github.com/repos/propelorm/Propel2/issues/1180/comments","events_url":"https://api.github.com/repos/propelorm/Propel2/issues/1180/events","html_url":"https://github.com/propelorm/Propel2/issues/1180","id":152960426,"number":1180,"title":"Simultaneous creation on call to findOneOrCreate() causes fatal error?","user":{"login":"benguild","id":1185659,"avatar_url":"https://avatars.githubusercontent.com/u/1185659?v=3","gravatar_id":"","url":"https://api.github.com/users/benguild","html_url":"https://github.com/benguild","followers_url":"https://api.github.com/users/benguild/followers","following_url":"https://api.github.com/users/benguild/following{/other_user}","gists_url":"https://api.github.com/users/benguild/gists{/gist_id}","starred_url":"https://api.github.com/users/benguild/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benguild/subscriptions","organizations_url":"https://api.github.com/users/benguild/orgs","repos_url":"https://api.github.com/users/benguild/repos","events_url":"https://api.github.com/users/benguild/events{/privacy}","received_events_url":"https://api.github.com/users/benguild/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-05-04T08:45:57Z","updated_at":"2016-05-04T22:01:37Z","closed_at":null,"body":"I've found that if another process creates a row between the SELECT and INSERT calls of `findOneOrCreate()`, the script crashes for \"Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry\". This doesn't really make sense in a `findOneOrCreate()` situation, no?\r\n\r\nTo me, it seems like both an edge case and undesirable behavior for the crash to occur. Yet, wrapping absolutely every single `findOneOrCreate()` with a try/catch seems stupid... yet also a valid workaround. At least for the ones with possible collisions.\r\n\r\nShouldn't it do SELECT -> INSERT -> SELECT ... **then** throw any exception from the INSERT if the row still does not exist?"},"comment":{"url":"https://api.github.com/repos/propelorm/Propel2/issues/comments/217017107","html_url":"https://github.com/propelorm/Propel2/issues/1180#issuecomment-217017107","issue_url":"https://api.github.com/repos/propelorm/Propel2/issues/1180","id":217017107,"user":{"login":"marcj","id":450980,"avatar_url":"https://avatars.githubusercontent.com/u/450980?v=3","gravatar_id":"","url":"https://api.github.com/users/marcj","html_url":"https://github.com/marcj","followers_url":"https://api.github.com/users/marcj/followers","following_url":"https://api.github.com/users/marcj/following{/other_user}","gists_url":"https://api.github.com/users/marcj/gists{/gist_id}","starred_url":"https://api.github.com/users/marcj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcj/subscriptions","organizations_url":"https://api.github.com/users/marcj/orgs","repos_url":"https://api.github.com/users/marcj/repos","events_url":"https://api.github.com/users/marcj/events{/privacy}","received_events_url":"https://api.github.com/users/marcj/received_events","type":"User","site_admin":false},"created_at":"2016-05-04T22:01:37Z","updated_at":"2016-05-04T22:01:37Z","body":"If you really can't make sure beforehand that you insert a valid object with a unique key you should try using [upserts](http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html)."}}