{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/3931","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/3931/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/3931/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/3931/events","html_url":"https://github.com/sequelize/sequelize/issues/3931","id":88112073,"number":3931,"title":"Multiple asynchronous findOrCreate with the same term doesn't find properly","user":{"login":"phanect","id":315601,"avatar_url":"https://avatars.githubusercontent.com/u/315601?v=3","gravatar_id":"","url":"https://api.github.com/users/phanect","html_url":"https://github.com/phanect","followers_url":"https://api.github.com/users/phanect/followers","following_url":"https://api.github.com/users/phanect/following{/other_user}","gists_url":"https://api.github.com/users/phanect/gists{/gist_id}","starred_url":"https://api.github.com/users/phanect/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phanect/subscriptions","organizations_url":"https://api.github.com/users/phanect/orgs","repos_url":"https://api.github.com/users/phanect/repos","events_url":"https://api.github.com/users/phanect/events{/privacy}","received_events_url":"https://api.github.com/users/phanect/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-14T03:35:06Z","updated_at":"2015-06-14T12:30:19Z","closed_at":null,"body":"I wrote following code:\r\n\r\n```javascript\r\nvar User = this.sequelize.define('User', {\r\n  email: {\r\n    type: DataTypes.STRING\r\n  }\r\n});\r\n\r\nreturn Promise.all([\r\n  User.findOrCreate({\r\n    where: { email: 'email@sequelizejs.com' }\r\n  }),\r\n  User.findOrCreate({\r\n    where: { email: 'email@sequelizejs.com' }\r\n  }),\r\n  User.findOrCreate({\r\n    where: { email: 'email@sequelizejs.com' }\r\n  })\r\n])\r\n```\r\n\r\nIn above code, 3 `findOrCreate` with the same search terms runs asynchronously.\r\nI expected only 1 `User` is created, but actually 3 `User`s are created.\r\n\r\nAccording to the log, SQL seems running with following order:\r\n\r\n```\r\nSELECT `email` FROM `User` AS `User` WHERE (`User`.`email` = 'email@sequelizejs.com', ...) LIMIT 1;\r\n# Empty Set\r\nSELECT `email` FROM `User` AS `User` WHERE (`User`.`email` = 'email@sequelizejs.com', ...) LIMIT 1;\r\n# Empty Set\r\nSELECT `email` FROM `User` AS `User` WHERE (`User`.`email` = 'email@sequelizejs.com', ...) LIMIT 1;\r\n# Empty Set\r\nINSERT INTO `User` (`email`, ...) VALUES ('email@sequelizejs.com', ...);\r\nINSERT INTO `User` (`email`, ...) VALUES ('email@sequelizejs.com', ...);\r\nINSERT INTO `User` (`email`, ...) VALUES ('email@sequelizejs.com', ...);\r\n```\r\n\r\n(Above log is written manually based on the log of another example)\r\nSo `findOrCreate` seems to allow another query during the time between `find` and `create`.\r\n\r\nExpected SQL order is:\r\n\r\n```\r\nSELECT `email` FROM `User` AS `User` WHERE (`User`.`email` = 'email@sequelizejs.com', ...) LIMIT 1;\r\n# Empty Set\r\nINSERT INTO `User` (`email`, ...) VALUES ('email@sequelizejs.com', ...);\r\nSELECT `email` FROM `User` AS `User` WHERE (`User`.`email` = 'email@sequelizejs.com', ...) LIMIT 1;\r\n# User record returned; No INSERT run\r\nSELECT `email` FROM `User` AS `User` WHERE (`User`.`email` = 'email@sequelizejs.com', ...) LIMIT 1;\r\n# User record returned; No INSERT run\r\n```"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/111822294","html_url":"https://github.com/sequelize/sequelize/issues/3931#issuecomment-111822294","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/3931","id":111822294,"user":{"login":"janmeier","id":1210224,"avatar_url":"https://avatars.githubusercontent.com/u/1210224?v=3","gravatar_id":"","url":"https://api.github.com/users/janmeier","html_url":"https://github.com/janmeier","followers_url":"https://api.github.com/users/janmeier/followers","following_url":"https://api.github.com/users/janmeier/following{/other_user}","gists_url":"https://api.github.com/users/janmeier/gists{/gist_id}","starred_url":"https://api.github.com/users/janmeier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/janmeier/subscriptions","organizations_url":"https://api.github.com/users/janmeier/orgs","repos_url":"https://api.github.com/users/janmeier/repos","events_url":"https://api.github.com/users/janmeier/events{/privacy}","received_events_url":"https://api.github.com/users/janmeier/received_events","type":"User","site_admin":false},"created_at":"2015-06-14T12:30:19Z","updated_at":"2015-06-14T12:30:19Z","body":"Adding unique: true to the email field will fix the issue. Tagging this as a documentation issue, findOrCreate can only handle concurrent duplicates when there is a unique index\r\n"}}