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 exis
| 1365 | |
| 1366 | |
| 1367 | class Enum(String, SchemaType, Emulated, TypeEngine[Union[str, enum.Enum]]): |
| 1368 | """Generic Enum Type. |
| 1369 | |
| 1370 | The :class:`.Enum` type provides a set of possible string values |
| 1371 | which the column is constrained towards. |
| 1372 | |
| 1373 | The :class:`.Enum` type will make use of the backend's native "ENUM" |
| 1374 | type if one is available; otherwise, it uses a VARCHAR datatype. |
| 1375 | An option also exists to automatically produce a CHECK constraint |
| 1376 | when the VARCHAR (so called "non-native") variant is produced; |
| 1377 | see the :paramref:`.Enum.create_constraint` flag. |
| 1378 | |
| 1379 | The :class:`.Enum` type also provides in-Python validation of string |
| 1380 | values during both read and write operations. When reading a value |
| 1381 | from the database in a result set, the string value is always checked |
| 1382 | against the list of possible values and a ``LookupError`` is raised |
| 1383 | if no match is found. When passing a value to the database as a |
| 1384 | plain string within a SQL statement, if the |
| 1385 | :paramref:`.Enum.validate_strings` parameter is |
| 1386 | set to True, a ``LookupError`` is raised for any string value that's |
| 1387 | not located in the given list of possible values; note that this |
| 1388 | impacts usage of LIKE expressions with enumerated values (an unusual |
| 1389 | use case). |
| 1390 | |
| 1391 | The source of enumerated values may be a list of string values, or |
| 1392 | alternatively a PEP-435-compliant enumerated class. For the purposes |
| 1393 | of the :class:`.Enum` datatype, this class need only provide a |
| 1394 | ``__members__`` method. |
| 1395 | |
| 1396 | When using an enumerated class, the enumerated objects are used |
| 1397 | both for input and output, rather than strings as is the case with |
| 1398 | a plain-string enumerated type:: |
| 1399 | |
| 1400 | import enum |
| 1401 | from sqlalchemy import Enum |
| 1402 | |
| 1403 | |
| 1404 | class MyEnum(enum.Enum): |
| 1405 | one = 1 |
| 1406 | two = 2 |
| 1407 | three = 3 |
| 1408 | |
| 1409 | |
| 1410 | t = Table("data", MetaData(), Column("value", Enum(MyEnum))) |
| 1411 | |
| 1412 | connection.execute(t.insert(), {"value": MyEnum.two}) |
| 1413 | assert connection.scalar(t.select()) is MyEnum.two |
| 1414 | |
| 1415 | Above, the string names of each element, e.g. "one", "two", "three", |
| 1416 | are persisted to the database; the values of the Python Enum, here |
| 1417 | indicated as integers, are **not** used; the value of each enum can |
| 1418 | therefore be any kind of Python object whether or not it is persistable. |
| 1419 | |
| 1420 | In order to persist the values and not the names, the |
| 1421 | :paramref:`.Enum.values_callable` parameter may be used. The value of |
| 1422 | this parameter is a user-supplied callable, which is intended to be used |
| 1423 | with a PEP-435-compliant enumerated class and returns a list of string |
| 1424 | values to be persisted. For a simple enumeration that uses string values, |
no outgoing calls