MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / order_by

Method order_by

lib/sqlalchemy/orm/query.py:2027–2076  ·  lib/sqlalchemy/orm/query.py::Query.order_by

Apply one or more ORDER BY criteria to the query and return the newly resulting :class:`_query.Query`. e.g.:: q = session.query(Entity).order_by(Entity.id, Entity.name) Calling this method multiple times is equivalent to calling it once with all the cla

(
        self,
        __first: Union[
            Literal[None, False, _NoArg.NO_ARG],
            _ColumnExpressionOrStrLabelArgument[Any],
        ] = _NoArg.NO_ARG,
        /,
        *clauses: _ColumnExpressionOrStrLabelArgument[Any],
    )

Source from the content-addressed store, hash-verified

2025
2026 @_generative
2027 def order_by(
2028 self,
2029 __first: Union[
2030 Literal[None, False, _NoArg.NO_ARG],
2031 _ColumnExpressionOrStrLabelArgument[Any],
2032 ] = _NoArg.NO_ARG,
2033 /,
2034 *clauses: _ColumnExpressionOrStrLabelArgument[Any],
2035 ) -> Self:
2036 class="st">"""Apply one or more ORDER BY criteria to the query and return
2037 the newly resulting :class:`_query.Query`.
2038
2039 e.g.::
2040
2041 q = session.query(Entity).order_by(Entity.id, Entity.name)
2042
2043 Calling this method multiple times is equivalent to calling it once
2044 with all the clauses concatenated. All existing ORDER BY criteria may
2045 be cancelled by passing ``None`` by itself. New ORDER BY criteria may
2046 then be added by invoking :meth:`_orm.Query.order_by` again, e.g.::
2047
2048 class="cm"># will erase all ORDER BY and ORDER BY new_col alone
2049 q = q.order_by(None).order_by(new_col)
2050
2051 .. seealso::
2052
2053 These sections describe ORDER BY in terms of :term:`2.0 style`
2054 invocation but apply to :class:`_orm.Query` as well:
2055
2056 :ref:`tutorial_order_by` - in the :ref:`unified_tutorial`
2057
2058 :ref:`tutorial_order_by_label` - in the :ref:`unified_tutorial`
2059
2060 :meth:`_sql.Select.order_by` - v2 equivalent method.
2061
2062 class="st">"""
2063
2064 for assertion in (self._no_statement_condition, self._no_limit_offset):
2065 assertion(class="st">"order_by")
2066
2067 if not clauses and (__first is None or __first is False):
2068 self._order_by_clauses = ()
2069 elif __first is not _NoArg.NO_ARG:
2070 criterion = tuple(
2071 coercions.expect(roles.OrderByRole, clause)
2072 for clause in (__first,) + clauses
2073 )
2074 self._order_by_clauses += criterion
2075
2076 return self
2077
2078 @_generative
2079 def group_by(

Callers 15

get_schema_namesMethod · 0.45
_columns_queryMethod · 0.45
_constraint_queryMethod · 0.45
_foreing_key_queryMethod · 0.45
_index_queryMethod · 0.45
_enum_queryMethod · 0.45
_domain_queryMethod · 0.45
get_schema_namesMethod · 0.45
_column_queryMethod · 0.45
_index_queryMethod · 0.45
_constraint_queryMethod · 0.45

Calls

no outgoing calls

Tested by 15

test_collate_order_byMethod · 0.36
test_plainMethod · 0.36
test_composed_intMethod · 0.36
test_plain_descMethod · 0.36
test_simple_limitMethod · 0.36
test_simple_fetchMethod · 0.36
test_simple_offsetMethod · 0.36