From 361beb0bc8392c92403ffc1999eb2a9847e945c7 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Thu, 16 Feb 2023 21:52:18 +0100 Subject: Allow custom sorting of column in the ORM. To accommodate a change in column ordering used by ORM Declarative in SQLAlchemy 2.0, a new parameter :paramref:`_orm.mapped_column.sort_order` has been added that can be used to control the order of the columns defined in the table by the ORM, for common use cases such as mixins with primary key columns that should appear first in tables. The change notes at :ref:`change_9297` illustrate the default change in ordering behavior (which is part of all SQLAlchemy 2.0 releases) as well as use of the :paramref:`_orm.mapped_column.sort_order` to control column ordering when using mixins and multiple classes (new in 2.0.4). Fixes: #9297 Change-Id: Ic7163d64efdc0eccb53d6ae0dd89ec83427fb675 --- lib/sqlalchemy/sql/_typing.py | 2 ++ lib/sqlalchemy/sql/schema.py | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/_typing.py b/lib/sqlalchemy/sql/_typing.py index ab124103f..6bf9a5a1f 100644 --- a/lib/sqlalchemy/sql/_typing.py +++ b/lib/sqlalchemy/sql/_typing.py @@ -264,6 +264,8 @@ _EquivalentColumnMap = Dict["ColumnElement[Any]", Set["ColumnElement[Any]"]] _LimitOffsetType = Union[int, _ColumnExpressionArgument[int], None] +_AutoIncrementType = Union[bool, Literal["auto", "ignore_fk"]] + if TYPE_CHECKING: def is_sql_compiler(c: Compiled) -> TypeGuard[SQLCompiler]: diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 2a713fea6..20c0341ad 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -84,6 +84,7 @@ from ..util.typing import Self from ..util.typing import TypeGuard if typing.TYPE_CHECKING: + from ._typing import _AutoIncrementType from ._typing import _DDLColumnArgument from ._typing import _InfoType from ._typing import _TextCoercedExpressionArgument @@ -1375,7 +1376,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]): *args: SchemaEventTarget, name: Optional[str] = None, type_: Optional[_TypeEngineArgument[_T]] = None, - autoincrement: Union[bool, Literal["auto", "ignore_fk"]] = "auto", + autoincrement: _AutoIncrementType = "auto", default: Optional[Any] = None, doc: Optional[str] = None, key: Optional[str] = None, @@ -1949,7 +1950,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]): self.system = system self.doc = doc - self.autoincrement = autoincrement + self.autoincrement: _AutoIncrementType = autoincrement self.constraints = set() self.foreign_keys = set() self.comment = comment @@ -2278,7 +2279,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]): def _copy(self, **kw: Any) -> Column[Any]: """Create a copy of this ``Column``, uninitialized. - This is used in :meth:`_schema.Table.to_metadata`. + This is used in :meth:`_schema.Table.to_metadata` and by the ORM. """ -- cgit v1.2.1