sqlalchemy.types.
TypeEngine
¶Bases: sqlalchemy.sql.visitors.Visitable
The ultimate base class for all SQL datatypes.
Common subclasses of TypeEngine
include
String
, Integer
, and Boolean
.
For an overview of the SQLAlchemy typing system, see Column and Data Types.
See also
Comparator
(expr)¶Bases: sqlalchemy.sql.operators.ColumnOperators
Base class for custom comparison operations defined at the
type level. See TypeEngine.comparator_factory
.
TypeEngine.
adapt
(cls, **kw)¶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.
TypeEngine.
bind_expression
(bindvalue)¶“Given a bind value (i.e. a BindParameter
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.
The method is evaluated at statement compile time, as opposed to statement construction time.
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.
See also:
TypeEngine.
bind_processor
(dialect)¶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
.
Parameters: | dialect¶ – Dialect instance in use. |
---|
TypeEngine.
coerce_compared_value
(op, value)¶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
TypeDecorator.coerce_compared_value()
which is intended for
end-user customization of this behavior.
TypeEngine.
column_expression
(colexpr)¶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 TypeEngine.result_processor()
method.
The method is evaluated at statement compile time, as opposed to statement construction time.
See also:
TypeEngine.
comparator_factory
¶Bases: sqlalchemy.sql.operators.ColumnOperators
A TypeEngine.Comparator
class which will apply
to operations performed by owning ColumnElement
objects.
The comparator_factory
attribute is a hook consulted by
the core expression system when column and SQL expression operations
are performed. When a TypeEngine.Comparator
class is
associated with this attribute, it allows custom re-definition of
all existing operators, as well as definition of new operators.
Existing operators include those provided by Python operator overloading
such as operators.ColumnOperators.__add__()
and
operators.ColumnOperators.__eq__()
,
those provided as standard
attributes of operators.ColumnOperators
such as
operators.ColumnOperators.like()
and operators.ColumnOperators.in_()
.
Rudimentary usage of this hook is allowed through simple subclassing
of existing types, or alternatively by using TypeDecorator
.
See the documentation section Redefining and Creating New Operators for examples.
New in version 0.8: The expression system was enhanced to support customization of operators on a per-type level.
alias of Comparator
TypeEngine.
compare_against_backend
(dialect, conn_type)¶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 impement 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: |
---|
New in version 1.0.3.
TypeEngine.
compare_values
(x, y)¶Compare two values for equality.
TypeEngine.
compile
(dialect=None)¶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 Dialect instance. |
---|
TypeEngine.
dialect_impl
(dialect)¶Return a dialect-specific implementation for this
TypeEngine
.
TypeEngine.
evaluates_none
()¶Return a copy of this type which has the should_evaluate_none
flag set to True.
E.g.:
Table(
'some_table', metadata,
Column(
String(50).evaluates_none(),
nullable=True,
server_default='no value')
)
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 “evaulates none” flag does not apply to a value
of None
passed to Column.default
or
Column.server_default
; in these cases, None
still means “no default”.
New in version 1.1.
See also
Forcing NULL on a column with a default - in the ORM documentation
postgresql.JSON.none_as_null
- PostgreSQL JSON
interaction with this flag.
TypeEngine.should_evaluate_none
- class-level flag
TypeEngine.
get_dbapi_type
(dbapi)¶Return the corresponding type object from the underlying DB-API, if any.
This can be useful for callingsetinputsizes()
, for example.
TypeEngine.
hashable
= True¶Flag, if False, means values from this type aren’t hashable.
Used by the ORM when uniquing result lists.
TypeEngine.
literal_processor
(dialect)¶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.
New in version 0.9.0.
TypeEngine.
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.
TypeEngine.
result_processor
(dialect, coltype)¶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
.
Parameters: |
---|
TypeEngine.
should_evaluate_none
= False¶If True, the Python constant None
is considered to be handled
explicitly by this type.
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 types which have special
behavior for Python None, such as a JSON type, to indicate that
they’d like to handle the None value explicitly.
To set this flag on an existing type, use the
TypeEngine.evaluates_none()
method.
See also
New in version 1.1.
TypeEngine.
with_variant
(type_, dialect_name)¶Produce a new type object that will utilize the given type when applied to the dialect of the given name.
e.g.:
from sqlalchemy.types import String
from sqlalchemy.dialects import mysql
s = String()
s = s.with_variant(mysql.VARCHAR(collation='foo'), 'mysql')
The construction of TypeEngine.with_variant()
is always
from the “fallback” type to that which is dialect specific.
The returned type is an instance of Variant
, which
itself provides a Variant.with_variant()
that can be called repeatedly.
Parameters: |
|
---|
New in version 0.7.2.
sqlalchemy.types.
Concatenable
¶A mixin that marks a type as supporting ‘concatenation’, typically strings.
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.
sqlalchemy.types.
NullType
¶Bases: sqlalchemy.types.TypeEngine
An unknown type.
NullType
is used as a default type for those cases where
a type cannot be determined, including:
Dialect
somecolumn == my_special_object
)Column
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.
NullType
will result in a CompileError
if the compiler
is asked to render the type itself, such as if it is used in a
cast()
operation or within a schema creation operation such as that
invoked by MetaData.create_all()
or the CreateTable
construct.
sqlalchemy.types.
Variant
(base, mapping)¶Bases: sqlalchemy.types.TypeDecorator
A wrapping type that selects among a variety of implementations based on dialect in use.
The Variant
type is typically constructed
using the TypeEngine.with_variant()
method.
New in version 0.7.2.
See also
TypeEngine.with_variant()
for an example of use.
Members: | with_variant, __init__ |
---|