diff --git a/src/sentry/db/models/manager/base_query_set.py b/src/sentry/db/models/manager/base_query_set.py index 538192ed5fc018..ef87e3d5b01040 100644 --- a/src/sentry/db/models/manager/base_query_set.py +++ b/src/sentry/db/models/manager/base_query_set.py @@ -43,14 +43,18 @@ def update_with_returning(self, returned_fields: list[str], **kwargs: Any) -> li query.add_update_values(kwargs) # type: ignore[attr-defined] # Inline annotations in order_by(), if possible. - new_order_by = [] + new_order_by: list[Any] = [] for col in query.order_by: - if annotation := query.annotations.get(col): - if getattr(annotation, "contains_aggregate", False): - raise exceptions.FieldError( - f"Cannot update when ordering by an aggregate: {annotation}" - ) - new_order_by.append(annotation) + if isinstance(col, str): + annotation = query.annotations.get(col) + if annotation is not None: + if getattr(annotation, "contains_aggregate", False): + raise exceptions.FieldError( + f"Cannot update when ordering by an aggregate: {annotation}" + ) + new_order_by.append(annotation) + else: + new_order_by.append(col) else: new_order_by.append(col) query.order_by = tuple(new_order_by)