diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2019-08-27 19:29:21 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2019-08-27 19:29:21 +0000 |
| commit | 1e78d076cd022e76ead9dbae31be8ce866a79dbe (patch) | |
| tree | d6df993f5f852bc5fade5efa3c253ea9411474ec /lib/sqlalchemy/sql | |
| parent | 3980a9a455d08c5073cabc3c2b77de46fa36d7b4 (diff) | |
| parent | 75b2518b2659796c885396fd0893dd7f9b19a9ef (diff) | |
| download | sqlalchemy-1e78d076cd022e76ead9dbae31be8ce866a79dbe.tar.gz | |
Merge "Implement type-level sorting for Enum; apply to ORM primary keys"
Diffstat (limited to 'lib/sqlalchemy/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 22 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/type_api.py | 14 |
2 files changed, 36 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 631352ceb..fd15d7c79 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -20,6 +20,7 @@ from . import operators from . import roles from . import type_api from .base import _bind_or_error +from .base import NO_ARG from .base import SchemaEventTarget from .elements import _defer_name from .elements import quoted_name @@ -1356,6 +1357,19 @@ class Enum(Emulated, String, SchemaType): .. versionadded:: 1.2.3 + :param sort_key_function: a Python callable which may be used as the + "key" argument in the Python ``sorted()`` built-in. The SQLAlchemy + ORM requires that primary key columns which are mapped must + be sortable in some way. When using an unsortable enumeration + object such as a Python 3 ``Enum`` object, this parameter may be + used to set a default sort key function for the objects. By + default, the database value of the enumeration is used as the + sorting function. + + .. versionadded:: 1.3.8 + + + """ self._enum_init(enums, kw) @@ -1377,6 +1391,7 @@ class Enum(Emulated, String, SchemaType): self.native_enum = kw.pop("native_enum", True) self.create_constraint = kw.pop("create_constraint", True) self.values_callable = kw.pop("values_callable", None) + self._sort_key_function = kw.pop("sort_key_function", NO_ARG) values, objects = self._parse_into_values(enums, kw) self._setup_for_values(values, objects, kw) @@ -1450,6 +1465,13 @@ class Enum(Emulated, String, SchemaType): ) @property + def sort_key_function(self): + if self._sort_key_function is NO_ARG: + return self._db_value_for_elem + else: + return self._sort_key_function + + @property def native(self): return self.native_enum diff --git a/lib/sqlalchemy/sql/type_api.py b/lib/sqlalchemy/sql/type_api.py index 9838f0d5a..11407ad2e 100644 --- a/lib/sqlalchemy/sql/type_api.py +++ b/lib/sqlalchemy/sql/type_api.py @@ -135,6 +135,16 @@ class TypeEngine(Visitable): """ + sort_key_function = None + """A sorting function that can be passed as the key to sorted. + + The default value of ``None`` indicates that the values stored by + this type are self-sorting. + + .. versionadded:: 1.3.8 + + """ + should_evaluate_none = False """If True, the Python constant ``None`` is considered to be handled explicitly by this type. @@ -1354,6 +1364,10 @@ class TypeDecorator(SchemaEventTarget, TypeEngine): """ return self.impl.compare_values(x, y) + @property + def sort_key_function(self): + return self.impl.sort_key_function + def __repr__(self): return util.generic_repr(self, to_inspect=self.impl) |
