{"id":226,"date":"2013-08-02T10:39:12","date_gmt":"2013-08-02T10:39:12","guid":{"rendered":"http:\/\/www.ramandv.com\/blog\/?p=226"},"modified":"2013-08-02T10:39:12","modified_gmt":"2013-08-02T10:39:12","slug":"django-get_or_create-integrityerror-due-to-racing-condition","status":"publish","type":"post","link":"http:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/","title":{"rendered":"Django get_or_create IntegrityError due to racing condition"},"content":{"rendered":"\n<!-- Quick Adsense WordPress Plugin: http:\/\/quickadsense.com\/ -->\n<div class=\"e9e85ee105af8a6a4a383deebf1fa216\" data-index=\"1\" style=\"float: none; margin:10px 0 10px 0; text-align:center;\">\n<script type=\"text\/javascript\"><!--\r\ngoogle_ad_client = \"ca-pub-4917982260456644\";\r\n\/* ramandv.com-ad1 *\/\r\ngoogle_ad_slot = \"9226290554\";\r\ngoogle_ad_width = 728;\r\ngoogle_ad_height = 90;\r\n\/\/-->\r\n<\/script>\r\n<script type=\"text\/javascript\"\r\nsrc=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\r\n<\/script>\n<\/div>\n<p>In Django, get_or_create is not a thread safe method. So, it might results in racing condition. There is no standard solution available. More info on this issue in <a href=\"https:\/\/code.djangoproject.com\/ticket\/13906\">https:\/\/code.djangoproject.com\/ticket\/13906<\/a><\/p>\n<p>Possible solutions from <a href=\"http:\/\/stackoverflow.com\/questions\/2235318\/how-do-i-deal-with-this-race-condition-in-django\">stackoverflow answer<\/a>, and also better explanation about the problem<\/p>\n<blockquote>\n<p>Basically get_or_create can fail &#8211; if you take a look at its source,<br \/>\n  the you&#8217;ll see that it&#8217;s: get, if-problem: save+some_trickery,<br \/>\n  if-still-problem: get again, if-still-problem: surrender and raise.<\/p>\n<p>This means that if there are two simultaneous threads (or processes)<br \/>\n  running create_or_update_myobj, both trying to get_or_create the same<br \/>\n  object, then:<\/p>\n<p>first thread tries to get it &#8211; but it doesn&#8217;t yet exists, so, the<br \/>\n  thread tries to create it, but before the object is created&#8230;<br \/>\n  &#8230;second thread tries to get it &#8211; and this obviously fails now,<br \/>\n  because of the default AUTOCOMMIT=OFF for MySQLdb database connection,<br \/>\n  and REPEATABLE READ serializable level, both threads have frozen their<br \/>\n  views of MyObj table. subsequently, first thread creates its object<br \/>\n  and returns it gracefully, but&#8230; &#8230;second thread cannot create<br \/>\n  anything as it would violate unique constraint what&#8217;s funny,<br \/>\n  subsequent get on the second thread doesn&#8217;t see the object created in<br \/>\n  the first thread, due to the frozen view of MyObj table So, if you<br \/>\n  want to safely get_or_create anything, try something like this:<\/p>\n<\/blockquote>\n<pre><code> @transaction.commit_on_success\n def my_get_or_create(...):\n     try:\n         obj = MyObj.objects.create(...)\n     except IntegrityError:\n         transaction.commit()\n         obj = MyObj.objects.get(...)\n     return obj\n<\/code><\/pre>\n<p>Transaction.commit_on_success helps to get rid of frozen view of MyObj by creating new transaction.<\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In Django, get_or_create is not a thread safe method. So, it might results in racing condition. There is no standard solution available. More info on this issue in https:\/\/code.djangoproject.com\/ticket\/13906 Possible solutions from stackoverflow answer, and also better explanation about the &hellip; <a href=\"http:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[8],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django get_or_create IntegrityError due to racing condition - Articles for Developers\" \/>\n<meta property=\"og:description\" content=\"In Django, get_or_create is not a thread safe method. So, it might results in racing condition. There is no standard solution available. More info on this issue in https:\/\/code.djangoproject.com\/ticket\/13906 Possible solutions from stackoverflow answer, and also better explanation about the &hellip; Continue reading &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/\" \/>\n<meta property=\"og:site_name\" content=\"Articles for Developers\" \/>\n<meta property=\"article:published_time\" content=\"2013-08-02T10:39:12+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ramandv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.ramandv.com\/blog\/#website\",\"url\":\"http:\/\/www.ramandv.com\/blog\/\",\"name\":\"Articles for Developers\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.ramandv.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/#webpage\",\"url\":\"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/\",\"name\":\"Django get_or_create IntegrityError due to racing condition - Articles for Developers\",\"isPartOf\":{\"@id\":\"http:\/\/www.ramandv.com\/blog\/#website\"},\"datePublished\":\"2013-08-02T10:39:12+00:00\",\"dateModified\":\"2013-08-02T10:39:12+00:00\",\"author\":{\"@id\":\"http:\/\/www.ramandv.com\/blog\/#\/schema\/person\/ca1ede7143462f244081566e6ff97658\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Django get_or_create IntegrityError due to racing condition\"}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.ramandv.com\/blog\/#\/schema\/person\/ca1ede7143462f244081566e6ff97658\",\"name\":\"ramandv\",\"url\":\"http:\/\/www.ramandv.com\/blog\/author\/ramandv\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/","og_locale":"en_US","og_type":"article","og_title":"Django get_or_create IntegrityError due to racing condition - Articles for Developers","og_description":"In Django, get_or_create is not a thread safe method. So, it might results in racing condition. There is no standard solution available. More info on this issue in https:\/\/code.djangoproject.com\/ticket\/13906 Possible solutions from stackoverflow answer, and also better explanation about the &hellip; Continue reading &rarr;","og_url":"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/","og_site_name":"Articles for Developers","article_published_time":"2013-08-02T10:39:12+00:00","twitter_misc":{"Written by":"ramandv","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/www.ramandv.com\/blog\/#website","url":"http:\/\/www.ramandv.com\/blog\/","name":"Articles for Developers","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.ramandv.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/#webpage","url":"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/","name":"Django get_or_create IntegrityError due to racing condition - Articles for Developers","isPartOf":{"@id":"http:\/\/www.ramandv.com\/blog\/#website"},"datePublished":"2013-08-02T10:39:12+00:00","dateModified":"2013-08-02T10:39:12+00:00","author":{"@id":"http:\/\/www.ramandv.com\/blog\/#\/schema\/person\/ca1ede7143462f244081566e6ff97658"},"breadcrumb":{"@id":"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ramandv.com\/blog\/django-get_or_create-integrityerror-due-to-racing-condition\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Django get_or_create IntegrityError due to racing condition"}]},{"@type":"Person","@id":"http:\/\/www.ramandv.com\/blog\/#\/schema\/person\/ca1ede7143462f244081566e6ff97658","name":"ramandv","url":"http:\/\/www.ramandv.com\/blog\/author\/ramandv\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6YZCe-3E","_links":{"self":[{"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/posts\/226"}],"collection":[{"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":2,"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":228,"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/posts\/226\/revisions\/228"}],"wp:attachment":[{"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ramandv.com\/blog\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}