summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2025-02-17 19:27:21 -0500
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-06-20 08:25:22 +0200
commit8ede411a81b40ca53362e6788601193c7e56a0cf (patch)
treefa68d8f108d56fcdbcc7ad29ae1a0442260bb50e /django/db/models/sql/query.py
parent56f468681aaedefcdada1b86c441c12ae96e7fea (diff)
Fixed #36152 -- Deprecated use of "%" in column aliases.
Unintentional support existed only on SQLite and Oracle.
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 187349a460..20dbf7cfaa 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -10,7 +10,9 @@ all about the internals of models in order to get the information it needs.
import copy
import difflib
import functools
+import inspect
import sys
+import warnings
from collections import Counter, namedtuple
from collections.abc import Iterator, Mapping
from itertools import chain, count, product
@@ -42,12 +44,17 @@ from django.db.models.query_utils import (
from django.db.models.sql.constants import INNER, LOUTER, ORDER_DIR, SINGLE
from django.db.models.sql.datastructures import BaseTable, Empty, Join, MultiJoin
from django.db.models.sql.where import AND, OR, ExtraWhere, NothingNode, WhereNode
+from django.utils.deprecation import RemovedInDjango70Warning
from django.utils.functional import cached_property
from django.utils.regex_helper import _lazy_re_compile
from django.utils.tree import Node
__all__ = ["Query", "RawQuery"]
+# RemovedInDjango70Warning: When the deprecation ends, replace with:
+# Quotation marks ('"`[]), whitespace characters, semicolons, percent signs
+# or inline SQL comments are forbidden in column aliases.
+# FORBIDDEN_ALIAS_PATTERN = _lazy_re_compile(r"['`\"\]\[;\s]|%|--|/\*|\*/")
# Quotation marks ('"`[]), whitespace characters, semicolons, or inline
# SQL comments are forbidden in column aliases.
FORBIDDEN_ALIAS_PATTERN = _lazy_re_compile(r"['`\"\]\[;\s]|--|/\*|\*/")
@@ -1206,9 +1213,23 @@ class Query(BaseExpression):
return alias or seen[None]
def check_alias(self, alias):
+ # RemovedInDjango70Warning: When the deprecation ends, remove.
+ if "%" in alias:
+ if "aggregate" in {frame.function for frame in inspect.stack()}:
+ stacklevel = 5
+ else:
+ # annotate() and alias().
+ stacklevel = 6
+ warnings.warn(
+ "Using percent signs in a column alias is deprecated.",
+ stacklevel=stacklevel,
+ category=RemovedInDjango70Warning,
+ )
if FORBIDDEN_ALIAS_PATTERN.search(alias):
raise ValueError(
"Column aliases cannot contain whitespace characters, quotation marks, "
+ # RemovedInDjango70Warning: When the deprecation ends, replace with:
+ # "semicolons, percent signs, or SQL comments."
"semicolons, or SQL comments."
)