Base Type API

    class sqlalchemy.types.TypeEngine

    The ultimate base class for all SQL datatypes.

    Common subclasses of TypeEngine include , Integer, and .

    For an overview of the SQLAlchemy typing system, see SQL Datatype Objects.

    See also

    Members

    operate(), , adapt(), , bind_expression(), , coerce_compared_value(), , comparator_factory, , compare_values(), , dialect_impl(), , get_dbapi_type(), , literal_processor(), , render_bind_cast, , result_processor(), , sort_key_function,

    Class signature

    class sqlalchemy.types.TypeEngine (, )

    • class Comparator

      Base class for custom comparison operations defined at the type level. See TypeEngine.comparator_factory.

      Class signature

      class (sqlalchemy.sql.expression.ColumnOperators, typing.Generic)

      • method operate(op: OperatorType, *other: Any, **kwargs: Any) → ColumnElement[_CT]

        Operate on an argument.

        This is the lowest level of operation, raises NotImplementedError by default.

        Overriding this on a subclass can allow common behavior to be applied to all operations. For example, overriding to apply func.lower() to the left and right side:

        Parameters:

        • op – Operator callable.

        • *other – the ‘other’ side of the operation. Will be a single scalar for most operations.

        • **kwargs – modifiers. These may be passed by special operators such as ColumnOperators.contains().

      • method sqlalchemy.types.TypeEngine.Comparator.reverse_operate(op: OperatorType, other: Any, **kwargs: Any) → [_CT]

        Reverse operate on an argument.

        Usage is the same as operate().

    • method adapt(cls: Type[Union[TypeEngine, TypeEngineMixin]], **kw: Any) →

      Produce an “adapted” form of this type, given an “impl” class to work with.

      This method is used internally to associate generic types with “implementation” types that are specific to a particular dialect.

    • method sqlalchemy.types.TypeEngine.as_generic(allow_nulltype: bool = False) →

      Return an instance of the generic type corresponding to this type using heuristic rule. The method may be overridden if this heuristic rule is not sufficient.

      1. >>> from sqlalchemy.dialects.mysql import INTEGER
      2. >>> INTEGER(display_width=4).as_generic()
      3. Integer()
      1. >>> from sqlalchemy.dialects.mysql import NVARCHAR
      2. >>> NVARCHAR(length=100).as_generic()
      3. Unicode(length=100)

      New in version 1.4.0b2.

      See also

      Reflecting with Database-Agnostic Types - describes the use of in conjunction with the DDLEvents.column_reflect() event, which is its intended use.

    • method sqlalchemy.types.TypeEngine.bind_expression(bindvalue: [_T]) → Optional[ColumnElement[_T]]

      Given a bind value (i.e. a instance), return a SQL expression in its place.

      This is typically a SQL function that wraps the existing bound parameter within the statement. It is used for special data types that require literals being wrapped in some special database function in order to coerce an application-level value into a database-specific format. It is the SQL analogue of the TypeEngine.bind_processor() method.

      This method is called during the SQL compilation phase of a statement, when rendering a SQL string. It is not called against specific values.

      Note that this method, when implemented, should always return the exact same structure, without any conditional logic, as it may be used in an executemany() call against an arbitrary number of bound parameter sets.

      Note

      This method is only called relative to a dialect specific type object, which is often private to a dialect in use and is not the same type object as the public facing one, which means it’s not feasible to subclass a class in order to provide an alternate TypeEngine.bind_expression() method, unless subclassing the class explicitly.

      To provide alternate behavior for TypeEngine.bind_expression(), implement a class and provide an implementation of TypeDecorator.bind_expression().

      See also

      See also

      Applying SQL-level Bind/Result Processing

    • method bind_processor(dialect: Dialect) → Optional[_BindProcessorType[_T]]

      Return a conversion function for processing bind values.

      Returns a callable which will receive a bind parameter value as the sole positional argument and will return a value to send to the DB-API.

      If processing is not necessary, the method should return None.

      Note

      This method is only called relative to a dialect specific type object, which is often private to a dialect in use and is not the same type object as the public facing one, which means it’s not feasible to subclass a class in order to provide an alternate TypeEngine.bind_processor() method, unless subclassing the class explicitly.

      To provide alternate behavior for TypeEngine.bind_processor(), implement a class and provide an implementation of TypeDecorator.process_bind_param().

      See also

      • Parameters:

        dialect – Dialect instance in use.

    • method sqlalchemy.types.TypeEngine.coerce_compared_value(op: Optional[OperatorType], value: Any) → [Any]

      Suggest a type for a ‘coerced’ Python value in an expression.

      Given an operator and value, gives the type a chance to return a type which the value should be coerced into.

      The default behavior here is conservative; if the right-hand side is already coerced into a SQL type based on its Python type, it is usually left alone.

      End-user functionality extension here should generally be via TypeDecorator, which provides more liberal behavior in that it defaults to coercing the other side of the expression into this type, thus applying special Python conversions above and beyond those needed by the DBAPI to both ides. It also provides the public method which is intended for end-user customization of this behavior.

    • method sqlalchemy.types.TypeEngine.column_expression(colexpr: [_T]) → Optional[ColumnElement[_T]]

      Given a SELECT column expression, return a wrapping SQL expression.

      This is typically a SQL function that wraps a column expression as rendered in the columns clause of a SELECT statement. It is used for special data types that require columns to be wrapped in some special database function in order to coerce the value before being sent back to the application. It is the SQL analogue of the method.

      This method is called during the SQL compilation phase of a statement, when rendering a SQL string. It is not called against specific values.

      Note

      This method is only called relative to a dialect specific type object, which is often private to a dialect in use and is not the same type object as the public facing one, which means it’s not feasible to subclass a TypeEngine class in order to provide an alternate method, unless subclassing the UserDefinedType class explicitly.

      To provide alternate behavior for , implement a TypeDecorator class and provide an implementation of .

      See also

      Augmenting Existing Types

      See also

    • attribute comparator_factory

      alias of Comparator

    • method compare_against_backend(dialect: Dialect, conn_type: [Any]) → Optional[bool]

      Compare this type against the given backend type.

      This function is currently not implemented for SQLAlchemy types, and for all built in types will return None. However, it can be implemented by a user-defined type where it can be consumed by schema comparison tools such as Alembic autogenerate.

      A future release of SQLAlchemy will potentially implement this method for builtin types as well.

      The function should return True if this type is equivalent to the given type; the type is typically reflected from the database so should be database specific. The dialect in use is also passed. It can also return False to assert that the type is not equivalent.

      • Parameters:

        • dialect – a Dialect that is involved in the comparison.

        • conn_type – the type object reflected from the backend.

    1. New in version 1.0.3.
    • method compare_values(x: Any, y: Any) → bool

      Compare two values for equality.

    • method sqlalchemy.types.TypeEngine.compile(dialect: Optional[] = None) → str

      Produce a string-compiled form of this TypeEngine.

      When called with no arguments, uses a “default” dialect to produce a string result.

      • Parameters:

        dialect – a instance.

    • method sqlalchemy.types.TypeEngine.dialect_impl(dialect: ) → TypeEngine[_T]

      Return a dialect-specific implementation for this .

    • method sqlalchemy.types.TypeEngine.evaluates_none() → SelfTypeEngine

      Return a copy of this type which has the flag set to True.

      E.g.:

      1. Table(
      2. 'some_table', metadata,
      3. Column(
      4. String(50).evaluates_none(),
      5. nullable=True,
      6. server_default='no value')
      7. )

      The ORM uses this flag to indicate that a positive value of None is passed to the column in an INSERT statement, rather than omitting the column from the INSERT statement which has the effect of firing off column-level defaults. It also allows for types which have special behavior associated with the Python None value to indicate that the value doesn’t necessarily translate into SQL NULL; a prime example of this is a JSON type which may wish to persist the JSON value 'null'.

      In all cases, the actual NULL SQL value can be always be persisted in any column by using the null SQL construct in an INSERT statement or associated with an ORM-mapped attribute.

      Note

      The “evaluates none” flag does not apply to a value of None passed to or Column.server_default; in these cases, None still means “no default”.

      New in version 1.1.

      See also

      - in the ORM documentation

      JSON.none_as_null - PostgreSQL JSON interaction with this flag.

      - class-level flag

    • method sqlalchemy.types.TypeEngine.get_dbapi_type(dbapi: module) → Optional[Any]

      Return the corresponding type object from the underlying DB-API, if any.

      This can be useful for calling setinputsizes(), for example.

    • attribute hashable = True

      Flag, if False, means values from this type aren’t hashable.

      Used by the ORM when uniquing result lists.

    • method sqlalchemy.types.TypeEngine.literal_processor(dialect: ) → Optional[_LiteralProcessorType[_T]]

      Return a conversion function for processing literal values that are to be rendered directly without using binds.

      This function is used when the compiler makes use of the “literal_binds” flag, typically used in DDL generation as well as in certain scenarios where backends don’t accept bound parameters.

      Returns a callable which will receive a literal Python value as the sole positional argument and will return a string representation to be rendered in a SQL statement.

      Note

      This method is only called relative to a dialect specific type object, which is often private to a dialect in use and is not the same type object as the public facing one, which means it’s not feasible to subclass a TypeEngine class in order to provide an alternate method, unless subclassing the UserDefinedType class explicitly.

      To provide alternate behavior for , implement a TypeDecorator class and provide an implementation of .

      See also

      Augmenting Existing Types

    • attribute python_type

      Return the Python type object expected to be returned by instances of this type, if known.

      Basically, for those types which enforce a return type, or are known across the board to do such for all common DBAPIs (like int for example), will return that type.

      If a return type is not defined, raises NotImplementedError.

      Note that any type also accommodates NULL in SQL which means you can also get back None from any type in practice.

    • attribute sqlalchemy.types.TypeEngine.render_bind_cast = False

      Render bind casts for mode.

      If True, this type (usually a dialect level impl type) signals to the compiler that a cast should be rendered around a bound parameter for this type.

      New in version 2.0.

      See also

      BindTyping

    • attribute render_literal_cast = False

      render casts when rendering a value as an inline literal, e.g. with TypeEngine.literal_processor().

      New in version 2.0.

    • method result_processor(dialect: Dialect, coltype: object) → Optional[_ResultProcessorType[_T]]

      Return a conversion function for processing result row values.

      Returns a callable which will receive a result row column value as the sole positional argument and will return a value to return to the user.

      If processing is not necessary, the method should return None.

      Note

      This method is only called relative to a dialect specific type object, which is often private to a dialect in use and is not the same type object as the public facing one, which means it’s not feasible to subclass a class in order to provide an alternate TypeEngine.result_processor() method, unless subclassing the class explicitly.

      To provide alternate behavior for TypeEngine.result_processor(), implement a class and provide an implementation of TypeDecorator.process_result_value().

      See also

      • Parameters:

        • dialect – Dialect instance in use.

        • coltype – DBAPI coltype argument received in cursor.description.

    • attribute sqlalchemy.types.TypeEngine.sort_key_function: Optional[Callable[[Any], Any]] = 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.

    • method with_variant(type\: _TypeEngineArgument[Any], *dialect_names: str_) → SelfTypeEngine

      Produce a copy of this type object that will utilize the given type when applied to the dialect of the given name.

      e.g.:

      The variant mapping indicates that when this type is interpreted by a specific dialect, it will instead be transmuted into the given type, rather than using the primary type.

      Changed in version 2.0: the TypeEngine.with_variant() method now works with a object “in place”, returning a copy of the original type rather than returning a wrapping object; the Variant class is no longer used.

      • Parameters:

        • type_ – a TypeEngine that will be selected as a variant from the originating type, when a dialect of the given name is in use.

        • *dialect_names

          one or more base names of the dialect which uses this type. (i.e. 'postgresql', 'mysql', etc.)

          Changed in version 2.0: multiple dialect names can be specified for one variant.

    1. See also
    2. [Using UPPERCASE and Backend-specific types for multiple backends]($6fe6521f7b4cdfa2.md#types-with-variant) - illustrates the use of [TypeEngine.with\_variant()](#sqlalchemy.types.TypeEngine.with_variant "sqlalchemy.types.TypeEngine.with_variant").

    class sqlalchemy.types.Concatenable

    A mixin that marks a type as supporting ‘concatenation’, typically strings.

    Members

    Class signature

    class sqlalchemy.types.Concatenable (sqlalchemy.types.TypeEngineMixin)

    class sqlalchemy.types.Indexable

    A mixin that marks a type as supporting indexing operations, such as array or JSON structures.

    New in version 1.1.0.

    Members

    comparator_factory

    Class signature

    class (sqlalchemy.types.TypeEngineMixin)

    class sqlalchemy.types.NullType

    An unknown type.

    is used as a default type for those cases where a type cannot be determined, including:

    • During table reflection, when the type of a column is not recognized by the Dialect

    • When constructing SQL expressions using plain Python objects of unknown types (e.g. )

    • When a new is created, and the given type is passed as None or is not passed at all.

    The NullType can be used within SQL expression invocation without issue, it just has no behavior either at the expression construction level or at the bind-parameter/result processing level. will result in a CompileError if the compiler is asked to render the type itself, such as if it is used in a operation or within a schema creation operation such as that invoked by MetaData.create_all() or the construct.

    Class signature

    class sqlalchemy.types.NullType ()

    class sqlalchemy.types.ExternalType

    mixin that defines attributes and behaviors specific to third-party datatypes.

    “Third party” refers to datatypes that are defined outside the scope of SQLAlchemy within either end-user application code or within external extensions to SQLAlchemy.

    Subclasses currently include TypeDecorator and .

    New in version 1.4.28.

    Members

    cache_ok

    Class signature

    class (sqlalchemy.types.TypeEngineMixin)

    • attribute sqlalchemy.types.ExternalType.cache_ok: Optional[bool] = None

      Indicate if statements using this are “safe to cache”.

      The default value None will emit a warning and then not allow caching of a statement which includes this type. Set to False to disable statements using this type from being cached at all without a warning. When set to True, the object’s class and selected elements from its state will be used as part of the cache key. For example, using a TypeDecorator:

      1. class MyType(TypeDecorator):
      2. impl = String
      3. cache_ok = True
      4. def __init__(self, choices):
      5. self.choices = tuple(choices)
      6. self.internal_only = True

      The cache key for the above type would be equivalent to:

      1. >>> MyType(["a", "b", "c"])._static_cache_key
      2. (<class '__main__.MyType'>, ('choices', ('a', 'b', 'c')))

      The caching scheme will extract attributes from the type that correspond to the names of parameters in the __init__() method. Above, the “choices” attribute becomes part of the cache key but “internal_only” does not, because there is no parameter named “internal_only”.

      The requirements for cacheable elements is that they are hashable and also that they indicate the same SQL rendered for expressions using this type every time for a given cache value.

      To accommodate for datatypes that refer to unhashable structures such as dictionaries, sets and lists, these objects can be made “cacheable” by assigning hashable structures to the attributes whose names correspond with the names of the arguments. For example, a datatype which accepts a dictionary of lookup values may publish this as a sorted series of tuples. Given a previously un-cacheable type as:

      1. class LookupType(UserDefinedType):
      2. '''a custom type that accepts a dictionary as a parameter.
      3. this is the non-cacheable version, as "self.lookup" is not
      4. hashable.
      5. '''
      6. def __init__(self, lookup):
      7. self.lookup = lookup
      8. def get_col_spec(self, **kw):
      9. return "VARCHAR(255)"
      10. def bind_processor(self, dialect):
      11. # ... works with "self.lookup" ...

      Where “lookup” is a dictionary. The type will not be able to generate a cache key:

      If we did set up such a cache key, it wouldn’t be usable. We would get a tuple structure that contains a dictionary inside of it, which cannot itself be used as a key in a “cache dictionary” such as SQLAlchemy’s statement cache, since Python dictionaries aren’t hashable:

      1. >>> # set cache_ok = True
      2. >>> type_.cache_ok = True
      3. >>> # this is the cache key it would generate
      4. >>> key = type_._static_cache_key
      5. (<class '__main__.LookupType'>, ('lookup', {'a': 10, 'b': 20}))
      6. >>> # however this key is not hashable, will fail when used with
      7. >>> # SQLAlchemy statement cache
      8. >>> some_cache = {key: "some sql value"}
      9. in <module> TypeError: unhashable type: 'dict'

      The type may be made cacheable by assigning a sorted tuple of tuples to the “.lookup” attribute:

      1. class LookupType(UserDefinedType):
      2. '''a custom type that accepts a dictionary as a parameter.
      3. The dictionary is stored both as itself in a private variable,
      4. and published in a public variable as a sorted tuple of tuples,
      5. which is hashable and will also return the same value for any
      6. two equivalent dictionaries. Note it assumes the keys and
      7. values of the dictionary are themselves hashable.
      8. '''
      9. cache_ok = True
      10. def __init__(self, lookup):
      11. self._lookup = lookup
      12. # assume keys/values of "lookup" are hashable; otherwise
      13. # they would also need to be converted in some way here
      14. self.lookup = tuple(
      15. (key, lookup[key]) for key in sorted(lookup)
      16. )
      17. def get_col_spec(self, **kw):
      18. return "VARCHAR(255)"
      19. def bind_processor(self, dialect):
      20. # ... works with "self._lookup" ...

      Where above, the cache key for LookupType({"a": 10, "b": 20}) will be:

      1. >>> LookupType({"a": 10, "b": 20})._static_cache_key
      2. (<class '__main__.LookupType'>, ('lookup', (('a', 10), ('b', 20))))

      New in version 1.4.14: - added the cache_ok flag to allow some configurability of caching for classes.

      New in version 1.4.28: - added the ExternalType mixin which generalizes the cache_ok flag to both the and UserDefinedType classes.

      See also

    class sqlalchemy.types.Variant

    deprecated. symbol is present for backwards-compatibility with workaround recipes, however this actual type should not be used.

    Members

    with_variant()

    Class signature

    class (sqlalchemy.types.TypeDecorator)

    • method with_variant(type\: _TypeEngineArgument[Any], *dialect_names: str_) → SelfTypeEngine

      inherited from the TypeEngine.with_variant() method of

      Produce a copy of this type object that will utilize the given type when applied to the dialect of the given name.

      e.g.:

      1. from sqlalchemy.types import String
      2. from sqlalchemy.dialects import mysql
      3. string_type = String()
      4. string_type = string_type.with_variant(
      5. mysql.VARCHAR(collation='foo'), 'mysql', 'mariadb'
      6. )

      The variant mapping indicates that when this type is interpreted by a specific dialect, it will instead be transmuted into the given type, rather than using the primary type.

      Changed in version 2.0: the TypeEngine.with_variant() method now works with a object “in place”, returning a copy of the original type rather than returning a wrapping object; the Variant class is no longer used.

      • Parameters:

        • type_ – a TypeEngine that will be selected as a variant from the originating type, when a dialect of the given name is in use.