class documentation

class UserDefinedType(util.with_metaclass(VisitableCheckKWArg, ExternalType, TypeEngine)):

View In Hierarchy

Base for user defined types.

This should be the base of new types. Note that for most cases, .TypeDecorator is probably more appropriate:

import sqlalchemy.types as types

class MyType(types.UserDefinedType):
    cache_ok = True

    def __init__(self, precision = 8):
        self.precision = precision

    def get_col_spec(self, **kw):
        return "MYTYPE(%s)" % self.precision

    def bind_processor(self, dialect):
        def process(value):
            return value
        return process

    def result_processor(self, dialect, coltype):
        def process(value):
            return value
        return process

Once the type is made, it's immediately usable:

table = Table('foo', metadata_obj,
    Column('id', Integer, primary_key=True),
    Column('data', MyType(16))
    )

The get_col_spec() method will in most cases receive a keyword argument type_expression which refers to the owning expression of the type as being compiled, such as a _schema.Column or .cast construct. This keyword is only sent if the method accepts keyword arguments (e.g. **kw) in its argument signature; introspection is used to check for this in order to support legacy forms of this function.

New in version 1.0.0: the owning expression is passed to the get_col_spec() method via the keyword argument type_expression, if it receives **kw in its signature.

The .UserDefinedType.cache_ok class-level flag indicates if this custom .UserDefinedType is safe to be used as part of a cache key. This flag defaults to None which will initially generate a warning when the SQL compiler attempts to generate a cache key for a statement that uses this type. If the .UserDefinedType is not guaranteed to produce the same bind/result behavior and SQL generation every time, this flag should be set to False; otherwise if the class produces the same behavior each time, it may be set to True. See .UserDefinedType.cache_ok for further notes on how this works.

New in version 1.4.28: Generalized the .ExternalType.cache_ok flag so that it is available for both .TypeDecorator as well as .UserDefinedType.
Method coerce​_compared​_value Suggest a type for a 'coerced' Python value in an expression.
Class Variable __visit​_name__ Undocumented
Class Variable ensure​_kwarg Undocumented
def coerce_compared_value(self, op, value):

Suggest a type for a 'coerced' Python value in an expression.

Default behavior for .UserDefinedType is the same as that of .TypeDecorator; by default it returns self, assuming the compared value should be coerced into the same type as this one. See .TypeDecorator.coerce_compared_value for more detail.

__visit_name__: str =

Undocumented

ensure_kwarg: str =

Undocumented