| 43 | |
| 44 | class SQLUpdateCompiler(BaseSQLUpdateCompiler): |
| 45 | def as_sql(self): |
| 46 | update_query, update_params = super().as_sql() |
| 47 | # MySQL and MariaDB support UPDATE ... ORDER BY syntax. |
| 48 | if self.query.order_by: |
| 49 | order_by_sql = [] |
| 50 | order_by_params = [] |
| 51 | db_table = self.query.get_meta().db_table |
| 52 | try: |
| 53 | for resolved, (sql, params, _) in self.get_order_by(): |
| 54 | if ( |
| 55 | isinstance(resolved.expression, Col) |
| 56 | and resolved.expression.alias != db_table |
| 57 | ): |
| 58 | # Ignore ordering if it contains joined fields, because |
| 59 | # they cannot be used in the ORDER BY clause. |
| 60 | raise FieldError |
| 61 | order_by_sql.append(sql) |
| 62 | order_by_params.extend(params) |
| 63 | update_query += " ORDER BY " + ", ".join(order_by_sql) |
| 64 | update_params += tuple(order_by_params) |
| 65 | except FieldError: |
| 66 | # Ignore ordering if it contains annotations, because they're |
| 67 | # removed in .update() and cannot be resolved. |
| 68 | pass |
| 69 | return update_query, update_params |