#35712: CheckConstraint with RawSQL breaks ModelForm when inside
transaction.atomic()
-------------------------------------+-------------------------------------
Reporter: a-p-f | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted
Comment:
Accepting on the basis that performing the validation, or not, should not
leave the database in a unusable state. A potential patch could be
{{{#!diff
diff --git a/django/db/models/query_utils.py
b/django/db/models/query_utils.py
index 1bf396723e..1b41314bc7 100644
--- a/django/db/models/query_utils.py
+++ b/django/db/models/query_utils.py
@@ -10,9 +10,10 @@
import inspect
import logging
from collections import namedtuple
+from contextlib import nullcontext
from django.core.exceptions import FieldError
-from django.db import DEFAULT_DB_ALIAS, DatabaseError, connections
+from django.db import DEFAULT_DB_ALIAS, DatabaseError, connections,
transaction
from django.db.models.constants import LOOKUP_SEP
from django.utils import tree
from django.utils.functional import cached_property
@@ -131,13 +132,20 @@ def check(self, against, using=DEFAULT_DB_ALIAS):
query.add_annotation(value, name, select=False)
query.add_annotation(Value(1), "_check")
# This will raise a FieldError if a field is missing in
"against".
- if connections[using].features.supports_comparing_boolean_expr:
+ connection = connections[using]
+ if connection.features.supports_comparing_boolean_expr:
query.add_q(Q(Coalesce(self, True,
output_field=BooleanField())))
else:
query.add_q(self)
compiler = query.get_compiler(using=using)
+ context_manager = (
+ transaction.atomic(using=using)
+ if connection.in_atomic_block
+ else nullcontext()
+ )
try:
- return compiler.execute_sql(SINGLE) is not None
+ with context_manager:
+ return compiler.execute_sql(SINGLE) is not None
except DatabaseError as e:
logger.warning("Got a database error calling check() on %r:
%s", self, e)
return True
}}}
If you're interested in submitting a PR it looks like
`test_q.QCheckTests.test_rawsql` could be adjusted to confirm that the
connection is still usable after database error.
--
Ticket URL: <https://code.djangoproject.com/ticket/35712#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107019194f853a6-8e7edb72-ac79-48c0-aa37-bd6e3923c12c-000000%40eu-central-1.amazonses.com.