Module « sqlalchemy »
Classe « Enum »
Informations générales
Héritage
builtins.object
SchemaEventTarget
SchemaType
builtins.object
Traversible
TypeEngine
builtins.object
Concatenable
String
builtins.object
Emulated
Enum
Définition
class Enum(Emulated, String, SchemaType):
Description [extrait de Enum.__doc__]
Generic Enum Type.
The :class:`.Enum` type provides a set of possible string values
which the column is constrained towards.
The :class:`.Enum` type will make use of the backend's native "ENUM"
type if one is available; otherwise, it uses a VARCHAR datatype.
An option also exists to automatically produce a CHECK constraint
when the VARCHAR (so called "non-native") variant is produced;
see the :paramref:`.Enum.create_constraint` flag.
The :class:`.Enum` type also provides in-Python validation of string
values during both read and write operations. When reading a value
from the database in a result set, the string value is always checked
against the list of possible values and a ``LookupError`` is raised
if no match is found. When passing a value to the database as a
plain string within a SQL statement, if the
:paramref:`.Enum.validate_strings` parameter is
set to True, a ``LookupError`` is raised for any string value that's
not located in the given list of possible values; note that this
impacts usage of LIKE expressions with enumerated values (an unusual
use case).
.. versionchanged:: 1.1 the :class:`.Enum` type now provides in-Python
validation of input values as well as on data being returned by
the database.
The source of enumerated values may be a list of string values, or
alternatively a PEP-435-compliant enumerated class. For the purposes
of the :class:`.Enum` datatype, this class need only provide a
``__members__`` method.
When using an enumerated class, the enumerated objects are used
both for input and output, rather than strings as is the case with
a plain-string enumerated type::
import enum
class MyEnum(enum.Enum):
one = 1
two = 2
three = 3
t = Table(
'data', MetaData(),
Column('value', Enum(MyEnum))
)
connection.execute(t.insert(), {"value": MyEnum.two})
assert connection.scalar(t.select()) is MyEnum.two
Above, the string names of each element, e.g. "one", "two", "three",
are persisted to the database; the values of the Python Enum, here
indicated as integers, are **not** used; the value of each enum can
therefore be any kind of Python object whether or not it is persistable.
In order to persist the values and not the names, the
:paramref:`.Enum.values_callable` parameter may be used. The value of
this parameter is a user-supplied callable, which is intended to be used
with a PEP-435-compliant enumerated class and returns a list of string
values to be persisted. For a simple enumeration that uses string values,
a callable such as ``lambda x: [e.value for e in x]`` is sufficient.
.. versionadded:: 1.1 - support for PEP-435-style enumerated
classes.
.. seealso::
:class:`_postgresql.ENUM` - PostgreSQL-specific type,
which has additional functionality.
:class:`.mysql.ENUM` - MySQL-specific type
Constructeur(s)
Liste des attributs statiques
dispatch | <sqlalchemy.event.base.DDLEventsDispatch object at 0x7f40cbc2af40> |
hashable | True |
RETURNS_BYTES | symbol('RETURNS_BYTES') |
RETURNS_CONDITIONAL | symbol('RETURNS_CONDITIONAL') |
RETURNS_UNICODE | symbol('RETURNS_UNICODE') |
RETURNS_UNKNOWN | symbol('RETURNS_UNKNOWN') |
should_evaluate_none | False |
Attributs statiques hérités de la classe String
sort_key_function
Liste des propriétés
bind | |
native | |
python_type | |
sort_key_function | |
Liste des opérateurs
Opérateurs hérités de la classe object
__eq__,
__ge__,
__gt__,
__le__,
__lt__,
__ne__
Liste des méthodes
Toutes les méthodes
Méthodes d'instance
Méthodes statiques
Méthodes dépréciées
__repr__(self) |
|
adapt(self, impltype, **kw) |
|
adapt_to_emulated(self, impltype, **kw) |
|
as_generic(self, allow_nulltype=False) |
|
bind_processor(self, dialect) |
|
Comparator(expr) |
|
comparator_factory(expr) |
|
copy(self, **kw) |
|
literal_processor(self, dialect) |
|
result_processor(self, dialect, coltype) |
|
Méthodes héritées de la classe SchemaType
__init_subclass__, __subclasshook__, create, drop
Méthodes héritées de la classe String
get_dbapi_type
Méthodes héritées de la classe TypeEngine
__str__, bind_expression, coerce_compared_value, column_expression, compare_against_backend, compare_values, compile, copy_value, dialect_impl, evaluates_none, with_variant
Méthodes héritées de la classe Traversible
__class_getitem__, get_children
Méthodes héritées de la classe object
__delattr__,
__dir__,
__format__,
__getattribute__,
__hash__,
__reduce__,
__reduce_ex__,
__setattr__,
__sizeof__
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :