summaryrefslogtreecommitdiff
path: root/django/db/models/sql/compiler.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2025-04-04 14:42:31 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2025-04-07 23:49:23 +0200
commit5d2a0c51d459379447563af3789a6d941ca4947c (patch)
tree7793f85ce46da02ae1cc6bde50357995f3d7bce7 /django/db/models/sql/compiler.py
parent77d2037511cc0152af6bea402bd09632ed7ca551 (diff)
[5.2.x] Fixed #36301 -- Fixed select_for_update(of) crash when using values()/values_list().
Regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a which allowed for annotations to be SELECT'ed before model field references through values()/values_list() and broke assumptions the select_for_update(of) table infererence logic had about model fields always being first. Refs #28900. Thanks OutOfFocus4 for the report and Sarah for the test. Backport of 71a19a0e475165dbc14c1fe02f552013ee670e4c from main
Diffstat (limited to 'django/db/models/sql/compiler.py')
-rw-r--r--django/db/models/sql/compiler.py23
1 files changed, 12 insertions, 11 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 0b6cfbfc37..4292243c71 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -256,17 +256,8 @@ class SQLCompiler:
# self.query.select is a special case. These columns never go to
# any model.
cols = self.query.select
- if cols:
- klass_info = {
- "model": self.query.model,
- "select_fields": list(
- range(
- len(self.query.extra_select),
- len(self.query.extra_select) + len(cols),
- )
- ),
- }
selected = []
+ select_fields = None
if self.query.selected is None:
selected = [
*(
@@ -276,18 +267,28 @@ class SQLCompiler:
*((None, col) for col in cols),
*self.query.annotation_select.items(),
]
+ select_fields = list(
+ range(
+ len(self.query.extra_select),
+ len(self.query.extra_select) + len(cols),
+ )
+ )
else:
- for alias, expression in self.query.selected.items():
+ select_fields = []
+ for index, (alias, expression) in enumerate(self.query.selected.items()):
# Reference to an annotation.
if isinstance(expression, str):
expression = self.query.annotations[expression]
# Reference to a column.
elif isinstance(expression, int):
+ select_fields.append(index)
expression = cols[expression]
# ColPairs cannot be aliased.
if isinstance(expression, ColPairs):
alias = None
selected.append((alias, expression))
+ if select_fields:
+ klass_info = {"model": self.query.model, "select_fields": select_fields}
for select_idx, (alias, expression) in enumerate(selected):
if alias: