{"id":251,"date":"2016-09-14T01:40:11","date_gmt":"2016-09-13T17:40:11","guid":{"rendered":"https:\/\/vinta.ws\/code\/?p=251"},"modified":"2026-02-18T01:20:36","modified_gmt":"2026-02-17T17:20:36","slug":"djangos-get_or_create-may-raise-integrityerror-but-subsequent-get-raises-doesnotexist","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/djangos-get_or_create-may-raise-integrityerror-but-subsequent-get-raises-doesnotexist.html","title":{"rendered":"Django's get_or_create() may raise IntegrityError but subsequent get() raises DoesNotExist"},"content":{"rendered":"<p>Django \u7684 <code>SomeModel.objects.get_or_create()<\/code> \u5be6\u969b\u4e0a\u7684\u6b65\u9a5f\u662f\uff1a<\/p>\n<ol>\n<li>get<\/li>\n<li>if problem: save + some_trickery<\/li>\n<li>if still problem: get again<\/li>\n<li>if still problem: surrender and raise<\/li>\n<\/ol>\n<p><code>get_or_create()<\/code> \u62cb\u51fa <code>IntegrityError<\/code> \u4f46\u662f\u63a5\u8457 <code>get()<\/code> \u4e00\u6b21\u537b\u6703\u662f <code>DoesNotExist<\/code>\uff0c\u9019\u500b\u932f\u8aa4\u5e38\u5e38\u6703\u767c\u751f\u5728 MySQL \u7684 <code>AUTOCOMMIT=OFF<\/code> \u4e14 isolation level \u662f REPEATABLE READ \u7684\u60c5\u6cc1\u4e0b\u3002MySQL \u9ed8\u8a8d\u63a1\u7528 REPEATABLE READ\uff08\u800c PostgreSQL \u9810\u8a2d\u662f READ COMMITTED\uff09\uff0c\u800c REPEATABLE READ \u7684\u5b9a\u7fa9\u662f\u5728\u540c\u4e00\u500b transaction \u4e4b\u5167 \u57f7\u884c\u76f8\u540c\u7684 SELECT \u4e00\u5b9a\u6703\u62ff\u5230\u76f8\u540c\u7684\u7d50\u679c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">with transaction.commit_on_success():\n    try:\n        user, created = User.objects.get_or_create(id=self.id, username=self.username)\n    except IntegrityError:\n        # \u9019\u500b get() \u53ef\u80fd\u6703\u767c\u751f DoesNotExist\n        user = User.objects.get(id=self.id)<\/code><\/pre>\n<p>\u6703\u767c\u751f\u90a3\u500b <code>DoesNotExist<\/code> \u901a\u5e38\u662f\u56e0\u70ba\uff1a<\/p>\n<ol>\n<li>thread a \u57f7\u884c <code>get()<\/code>\uff0c\u4f46\u662f <code>DoesNotExist<\/code><\/li>\n<li>\u6240\u4ee5 thread a \u63a5\u8457 <code>create()<\/code>\uff0c\u4f46\u662f\u5728\u8cc7\u6599\u5efa\u7acb\u4e4b\u524d<\/li>\n<li>thread b \u5e7e\u4e4e\u5728\u540c\u4e00\u500b\u6642\u9593\u9ede\u4e5f\u57f7\u884c <code>get()<\/code>\uff0c\u4e5f\u662f <code>DoesNotExist<\/code><\/li>\n<li>thread a \u6210\u529f\u5730 <code>create()<\/code> \u4e86<\/li>\n<li>\u56e0\u70ba\u5728 3 \u62ff\u4e0d\u5230\u8cc7\u6599\uff0c\u6240\u4ee5 thread b \u4e5f\u57f7\u884c <code>create()<\/code>\uff0c\u4f46\u662f\u56e0\u70ba UNIQUE CONSTRAINT\uff0c\u5c0e\u81f4\u5931\u6557\u4e86<\/li>\n<li>\u6240\u4ee5 thread b \u53c8\u6703 <code>get()<\/code> \u4e00\u6b21\uff0c\u4f46\u662f\u56e0\u70ba REPEATABLE READ\uff0cthread b \u4e0d\u6703\u62ff\u5230 thread a \u5efa\u7acb\u7684\u8cc7\u6599\uff0c\u6240\u4ee5 <code>DoesNotExist<\/code><\/li>\n<li>\u6240\u4ee5\u90a3\u500b <code>User.DoesNotExist<\/code> \u5c31\u662f thread b \u62cb\u51fa\u4f86\u7684\u932f\u8aa4<\/li>\n<li>thread a \u662f\u6210\u529f\u7684\uff0c\u4f60\u4e8b\u5f8c\u53bb\u8cc7\u6599\u5eab\u88e1\u67e5\u8a62\uff0c\u6703\u770b\u5230\u90a3\u500b User \u7684\u8cc7\u6599\u597d\u7aef\u7aef\u5730\u5728\u90a3\u88e1<\/li>\n<\/ol>\n<p>ref:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/2235318\/how-do-i-deal-with-this-race-condition-in-django\">http:\/\/stackoverflow.com\/questions\/2235318\/how-do-i-deal-with-this-race-condition-in-django<\/a><br \/>\n<a href=\"https:\/\/blog.ionelmc.ro\/2014\/12\/28\/terrible-choices-mysql\/\">https:\/\/blog.ionelmc.ro\/2014\/12\/28\/terrible-choices-mysql\/<\/a><\/p>\n<p>\u89e3\u6c7a\u7684\u65b9\u6cd5\uff0c\u9664\u4e86\u5c31\u4e7e\u8106\u4e0d\u8981\u5728\u540c\u4e00\u500b transaction \u88e1\u4e4b\u5916\uff0c\u57fa\u672c\u4e0a\u5c31\u53ea\u80fd\u628a isolation level \u6539\u6210 READ COMMITTED \u4e86\u3002\u4f3c\u4e4e\u6c92\u6709\u5176\u4ed6\u8fa6\u6cd5\u4e86\uff0c\u56e0\u70ba\u9019\u5c31\u662f isolation level \u7684\u5b9a\u7fa9\u3002<\/p>\n<p>As of MariaDB\/MySQL 5.1, if you use READ COMMITTED or enable innodb_locks_unsafe_for_binlog, you must use row-based binary logging.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">SHOW VARIABLES LIKE 'binlog_format';\nSET SESSION binlog_format = 'ROW';\nSET GLOBAL binlog_format = 'ROW';<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/dba.stackexchange.com\/questions\/2678\/how-do-i-show-the-binlog-format-on-a-mysql-server\">http:\/\/dba.stackexchange.com\/questions\/2678\/how-do-i-show-the-binlog-format-on-a-mysql-server<\/a><\/p>\n<p>Django \u548c Celery \u90fd\u5efa\u8b70\u4f7f\u7528 READ COMMITTED<br \/>\n<a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/ref\/models\/querysets\/#get-or-create\">https:\/\/docs.djangoproject.com\/en\/dev\/ref\/models\/querysets\/#get-or-create<\/a><br \/>\n<a href=\"https:\/\/code.djangoproject.com\/ticket\/13906\">https:\/\/code.djangoproject.com\/ticket\/13906<\/a><br \/>\n<a href=\"https:\/\/code.djangoproject.com\/ticket\/6641\">https:\/\/code.djangoproject.com\/ticket\/6641<\/a><br \/>\n<a href=\"http:\/\/docs.celeryproject.org\/en\/latest\/faq.html#mysql-is-throwing-deadlock-errors-what-can-i-do\">http:\/\/docs.celeryproject.org\/en\/latest\/faq.html#mysql-is-throwing-deadlock-errors-what-can-i-do<\/a><\/p>\n<h2>\u65b9\u6cd5\u4e00<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-sql\">SELECT @@GLOBAL.tx_isolation, @@tx_isolation;\n\n# MySQL\nSET SESSION tx_isolation='READ-COMMITTED';\nSET GLOBAL tx_isolation='READ-COMMITTED';\n\n# MariaDB\nSET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;\nSET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/set-transaction.html\">http:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/set-transaction.html<\/a><\/p>\n<h2>\u65b9\u6cd5\u4e8c<\/h2>\n<p>\/etc\/mysql\/my.cnf <\/p>\n<pre class=\"line-numbers\"><code class=\"language-ini\">[mysqld]\ntransaction-isolation = READ-COMMITTED<\/code><\/pre>\n<h2>\u65b9\u6cd5\u4e09<\/h2>\n<p>in settings.py<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.mysql',\n        ...\n        'OPTIONS': {\n            'init_command': 'SET SESSION tx_isolation=\"READ-COMMITTED\"',\n        },\n    }\n}<\/code><\/pre>\n<h2>\u65b9\u6cd5\u56db<\/h2>\n<p>\u81ea\u5df1\u5beb\u4e00\u500b <code>get_or_create()<\/code><\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">from django.db import transaction\n\n@transaction.atomic\n# or\n@transaction.commit_on_success\ndef 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<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/2235318\/how-do-i-deal-with-this-race-condition-in-django\">http:\/\/stackoverflow.com\/questions\/2235318\/how-do-i-deal-with-this-race-condition-in-django<\/a><\/p>\n<p>MySQL \u9ed8\u8a8d\u662f AUTOCOMMIT<br \/>\n<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/commit.html\">http:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/commit.html<\/a><\/p>\n<p>Django \u9ed8\u8a8d\u4e5f\u662f AUTOCOMMIT\uff0c\u9664\u975e\u4f60\u4f7f\u7528\u4e86 <code>TransactionMiddleware<\/code>\uff08Django 1.5 \u4ee5\u524d\uff09\u6216\u662f\u8a2d\u7f6e <code>ATOMIC_REQUESTS = False<\/code>\uff08Django 1.6 \u4ee5\u5f8c\uff09\u3002<code>TransactionMiddleware<\/code> \u7684\u6548\u679c\u985e\u4f3c <code>@transaction.commit_on_success<\/code> \u6216 <code>@transaction.atomic<\/code>\uff0c\u90fd\u662f\u628a AUTOCOMMIT \u95dc\u6389\uff0c\u5dee\u5225\u5728\u65bc\u7bc4\u570d\u4e0d\u540c\u3002<\/p>\n<p>ref:<br \/>\n<a href=\"http:\/\/django.readthedocs.io\/en\/1.5.x\/topics\/db\/transactions.html#django-s-default-transaction-behavior\">http:\/\/django.readthedocs.io\/en\/1.5.x\/topics\/db\/transactions.html#django-s-default-transaction-behavior<\/a><br \/>\n<a href=\"http:\/\/django.readthedocs.io\/en\/1.6.x\/topics\/db\/transactions.html#managing-autocommit\">http:\/\/django.readthedocs.io\/en\/1.6.x\/topics\/db\/transactions.html#managing-autocommit<\/a><br \/>\n<a href=\"http:\/\/django.readthedocs.io\/en\/1.6.x\/ref\/settings.html#std:setting-DATABASE-ATOMIC_REQUESTS\">http:\/\/django.readthedocs.io\/en\/1.6.x\/ref\/settings.html#std:setting-DATABASE-ATOMIC_REQUESTS<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>get_or_create() \u62cb\u51fa IntegrityError \u4f46\u662f\u63a5\u8457 get() \u4e00\u6b21\u537b\u6703\u662f DoesNotExist\uff0c\u4ee5\u4e0a\u7684\u932f\u8aa4\u5e38\u5e38\u6703\u767c\u751f\u5728 MySQL \u7684 isolation level \u662f REPEATABLE READ \u7684\u60c5\u6cc1\u4e0b\u3002MySQL \u9ed8\u8a8d\u63a1\u7528 REPEATABLE READ\uff08\u800c PostgreSQL \u9810\u8a2d\u662f READ COMMITTED\uff09\u3002<\/p>\n","protected":false},"author":1,"featured_media":275,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,4,116],"tags":[37,13,95,23,92],"class_list":["post-251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-database","category-about-python","category-about-web-development","tag-celery","tag-django","tag-django-models","tag-mysql","tag-transaction"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/275"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}