From db104ac091c2af34cbddc14d96f1af9c72f2864d Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Wed, 5 Aug 2026 04:29:48 -0600 Subject: [PATCH] =?UTF-8?q?fix(problem):=20resolve=5Ftags=20=E7=9A=84=20cr?= =?UTF-8?q?eate=20=E5=8C=85=E4=B8=80=E5=B1=82=20atomic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 外层若在事务里,IntegrityError 会中止整个事务,导致回查抛 TransactionManagementError。当前未开 ATOMIC_REQUESTS 所以还没触发, 但仓库多处用了 transaction.atomic(),属于随时会踩的坑。 Co-Authored-By: Claude Sonnet 5 --- problem/services.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/problem/services.py b/problem/services.py index 7a7a314..d72cf79 100644 --- a/problem/services.py +++ b/problem/services.py @@ -1,5 +1,5 @@ from django.core.cache import cache -from django.db import IntegrityError +from django.db import IntegrityError, transaction from utils.constants import CacheKey @@ -39,7 +39,10 @@ def resolve_tags(names): tag = ProblemTag.objects.filter(name__iexact=name).first() if tag is None: try: - tag = ProblemTag.objects.create(name=name) + # 包一层 atomic:外层若在事务里,IntegrityError 会中止整个事务, + # 导致下面的回查抛 TransactionManagementError + with transaction.atomic(): + tag = ProblemTag.objects.create(name=name) created = True except IntegrityError: # 并发下另一个请求刚建好同名标签,回查复用