SQLAlchemy provides abstractions for most common database data types, and a mechanism for specifying your own custom data types.
The methods and attributes of type objects are rarely used directly.
Type objects are supplied to Table
definitions
and can be supplied as type hints to functions for occasions where
the database driver returns an incorrect type.
>>> users = Table('users', metadata,
... Column('id', Integer, primary_key=True)
... Column('login', String(32))
... )
SQLAlchemy will use the Integer
and String(32)
type
information when issuing a CREATE TABLE
statement and will use it
again when reading back rows SELECTed
from the database.
Functions that accept a type (such as Column()
) will
typically accept a type class or instance; Integer
is equivalent
to Integer()
with no construction arguments in this case.
Generic types specify a column that can read, write and store a
particular type of Python data. SQLAlchemy will choose the best
database column type available on the target database when issuing a
CREATE TABLE
statement. For complete control over which column
type is emitted in CREATE TABLE
, such as VARCHAR
see SQL
Standard Types and the other sections of this chapter.
sqlalchemy.types.
BigInteger
¶Bases: sqlalchemy.types.Integer
A type for bigger int
integers.
Typically generates a BIGINT
in DDL, and otherwise acts like
a normal Integer
on the Python side.
sqlalchemy.types.
Boolean
(create_constraint=True, name=None, _create_events=True)¶Bases: sqlalchemy.types.TypeEngine
, sqlalchemy.types.SchemaType
A bool datatype.
Boolean typically uses BOOLEAN or SMALLINT on the DDL side, and on
the Python side deals in True
or False
.
__init__
(create_constraint=True, name=None, _create_events=True)¶Construct a Boolean.
Parameters: |
---|
sqlalchemy.types.
Date
¶Bases: sqlalchemy.types._DateAffinity
, sqlalchemy.types.TypeEngine
A type for datetime.date()
objects.
sqlalchemy.types.
DateTime
(timezone=False)¶Bases: sqlalchemy.types._DateAffinity
, sqlalchemy.types.TypeEngine
A type for datetime.datetime()
objects.
Date and time types return objects from the Python datetime
module. Most DBAPIs have built in support for the datetime
module, with the noted exception of SQLite. In the case of
SQLite, date and time types are stored as strings which are then
converted back to datetime objects when rows are returned.
sqlalchemy.types.
Enum
(*enums, **kw)¶Bases: sqlalchemy.types.String
, sqlalchemy.types.SchemaType
Generic Enum Type.
The Enum type provides a set of possible string values which the column is constrained towards.
By default, uses the backend’s native ENUM type if available, else uses VARCHAR + a CHECK constraint.
See also
ENUM
- PostgreSQL-specific type,
which has additional functionality.
__init__
(*enums, **kw)¶Construct an enum.
Keyword arguments which don’t apply to a specific backend are ignored by that backend.
Parameters: |
|
---|
create
(bind=None, checkfirst=False)¶Issue CREATE ddl for this type, if applicable.
drop
(bind=None, checkfirst=False)¶Issue DROP ddl for this type, if applicable.
sqlalchemy.types.
Float
(precision=None, asdecimal=False, decimal_return_scale=None, **kwargs)¶Bases: sqlalchemy.types.Numeric
Type representing floating point types, such as FLOAT
or REAL
.
This type returns Python float
objects by default, unless the
Float.asdecimal
flag is set to True, in which case they
are coerced to decimal.Decimal
objects.
Note
The Float
type is designed to receive data from a database
type that is explicitly known to be a floating point type
(e.g. FLOAT
, REAL
, others)
and not a decimal type (e.g. DECIMAL
, NUMERIC
, others).
If the database column on the server is in fact a Numeric
type, such as DECIMAL
or NUMERIC
, use the Numeric
type or a subclass, otherwise numeric coercion between
float
/Decimal
may or may not function as expected.
__init__
(precision=None, asdecimal=False, decimal_return_scale=None, **kwargs)¶Construct a Float.
Parameters: |
|
---|
sqlalchemy.types.
Integer
¶Bases: sqlalchemy.types._DateAffinity
, sqlalchemy.types.TypeEngine
A type for int
integers.
sqlalchemy.types.
Interval
(native=True, second_precision=None, day_precision=None)¶Bases: sqlalchemy.types._DateAffinity
, sqlalchemy.types.TypeDecorator
A type for datetime.timedelta()
objects.
The Interval type deals with datetime.timedelta
objects. In
PostgreSQL, the native INTERVAL
type is used; for others, the
value is stored as a date which is relative to the “epoch”
(Jan. 1, 1970).
Note that the Interval
type does not currently provide date arithmetic
operations on platforms which do not support interval types natively. Such
operations usually require transformation of both sides of the expression
(such as, conversion of both sides into integer epoch values first) which
currently is a manual procedure (such as via
func
).
__init__
(native=True, second_precision=None, day_precision=None)¶Construct an Interval object.
Parameters: |
|
---|
coerce_compared_value
(op, value)¶See TypeEngine.coerce_compared_value()
for a description.
sqlalchemy.types.
LargeBinary
(length=None)¶Bases: sqlalchemy.types._Binary
A type for large binary byte data.
The Binary type generates BLOB or BYTEA when tables are created,
and also converts incoming values using the Binary
callable
provided by each DB-API.
__init__
(length=None)¶Construct a LargeBinary type.
Parameters: | length¶ – optional, a length for the column for use in
DDL statements, for those BLOB types that accept a length
(i.e. MySQL). It does not produce a lengthed BINARY/VARBINARY
type - use the BINARY/VARBINARY types specifically for those.
May be safely omitted if no CREATE
TABLE will be issued. Certain databases may require a
length for use in DDL, and will raise an exception when
the CREATE TABLE DDL is issued. |
---|
sqlalchemy.types.
MatchType
(create_constraint=True, name=None, _create_events=True)¶Bases: sqlalchemy.types.Boolean
Refers to the return type of the MATCH operator.
As the ColumnOperators.match()
is probably the most open-ended
operator in generic SQLAlchemy Core, we can’t assume the return type
at SQL evaluation time, as MySQL returns a floating point, not a boolean,
and other backends might do something different. So this type
acts as a placeholder, currently subclassing Boolean
.
The type allows dialects to inject result-processing functionality
if needed, and on MySQL will return floating-point values.
New in version 1.0.0.
sqlalchemy.types.
Numeric
(precision=None, scale=None, decimal_return_scale=None, asdecimal=True)¶Bases: sqlalchemy.types._DateAffinity
, sqlalchemy.types.TypeEngine
A type for fixed precision numbers, such as NUMERIC
or DECIMAL
.
This type returns Python decimal.Decimal
objects by default, unless
the Numeric.asdecimal
flag is set to False, in which case
they are coerced to Python float
objects.
Note
The Numeric
type is designed to receive data from a database
type that is explicitly known to be a decimal type
(e.g. DECIMAL
, NUMERIC
, others) and not a floating point
type (e.g. FLOAT
, REAL
, others).
If the database column on the server is in fact a floating-point type
type, such as FLOAT
or REAL
, use the Float
type or a subclass, otherwise numeric coercion between
float
/Decimal
may or may not function as expected.
Note
The Python decimal.Decimal
class is generally slow
performing; cPython 3.3 has now switched to use the cdecimal library natively. For
older Python versions, the cdecimal
library can be patched
into any application where it will replace the decimal
library fully, however this needs to be applied globally and
before any other modules have been imported, as follows:
import sys
import cdecimal
sys.modules["decimal"] = cdecimal
Note that the cdecimal
and decimal
libraries are not
compatible with each other, so patching cdecimal
at the
global level is the only way it can be used effectively with
various DBAPIs that hardcode to import the decimal
library.
__init__
(precision=None, scale=None, decimal_return_scale=None, asdecimal=True)¶Construct a Numeric.
Parameters: |
|
---|
When using the Numeric
type, care should be taken to ensure
that the asdecimal setting is apppropriate for the DBAPI in use -
when Numeric applies a conversion from Decimal->float or float->
Decimal, this conversion incurs an additional performance overhead
for all result columns received.
DBAPIs that return Decimal natively (e.g. psycopg2) will have
better accuracy and higher performance with a setting of True
,
as the native translation to Decimal reduces the amount of floating-
point issues at play, and the Numeric type itself doesn’t need
to apply any further conversions. However, another DBAPI which
returns floats natively will incur an additional conversion
overhead, and is still subject to floating point data loss - in
which case asdecimal=False
will at least remove the extra
conversion overhead.
sqlalchemy.types.
PickleType
(protocol=2, pickler=None, comparator=None)¶Bases: sqlalchemy.types.TypeDecorator
Holds Python objects, which are serialized using pickle.
PickleType builds upon the Binary type to apply Python’s
pickle.dumps()
to incoming objects, and pickle.loads()
on
the way out, allowing any pickleable Python object to be stored as
a serialized binary field.
To allow ORM change events to propagate for elements associated
with PickleType
, see Mutation Tracking.
__init__
(protocol=2, pickler=None, comparator=None)¶Construct a PickleType.
Parameters: |
|
---|
impl
¶alias of LargeBinary
sqlalchemy.types.
SchemaType
(name=None, schema=None, metadata=None, inherit_schema=False, quote=None, _create_events=True)¶Bases: sqlalchemy.sql.expression.SchemaEventTarget
Mark a type as possibly requiring schema-level DDL for usage.
Supports types that must be explicitly created/dropped (i.e. PG ENUM type) as well as types that are complimented by table or schema level constraints, triggers, and other rules.
SchemaType
classes can also be targets for the
DDLEvents.before_parent_attach()
and
DDLEvents.after_parent_attach()
events, where the events fire off
surrounding the association of the type object with a parent
Column
.
adapt
(impltype, **kw)¶bind
¶copy
(**kw)¶create
(bind=None, checkfirst=False)¶Issue CREATE ddl for this type, if applicable.
drop
(bind=None, checkfirst=False)¶Issue DROP ddl for this type, if applicable.
sqlalchemy.types.
SmallInteger
¶Bases: sqlalchemy.types.Integer
A type for smaller int
integers.
Typically generates a SMALLINT
in DDL, and otherwise acts like
a normal Integer
on the Python side.
sqlalchemy.types.
String
(length=None, collation=None, convert_unicode=False, unicode_error=None, _warn_on_bytestring=False)¶Bases: sqlalchemy.types.Concatenable
, sqlalchemy.types.TypeEngine
The base for all string and character types.
In SQL, corresponds to VARCHAR. Can also take Python unicode objects and encode to the database’s encoding in bind params (and the reverse for result sets.)
The length field is usually required when the String type is used within a CREATE TABLE statement, as VARCHAR requires a length on most databases.
__init__
(length=None, collation=None, convert_unicode=False, unicode_error=None, _warn_on_bytestring=False)¶Create a string-holding type.
Parameters: |
|
---|
sqlalchemy.types.
Text
(length=None, collation=None, convert_unicode=False, unicode_error=None, _warn_on_bytestring=False)¶Bases: sqlalchemy.types.String
A variably sized string type.
In SQL, usually corresponds to CLOB or TEXT. Can also take Python unicode objects and encode to the database’s encoding in bind params (and the reverse for result sets.) In general, TEXT objects do not have a length; while some databases will accept a length argument here, it will be rejected by others.
sqlalchemy.types.
Time
(timezone=False)¶Bases: sqlalchemy.types._DateAffinity
, sqlalchemy.types.TypeEngine
A type for datetime.time()
objects.
sqlalchemy.types.
Unicode
(length=None, **kwargs)¶Bases: sqlalchemy.types.String
A variable length Unicode string type.
The Unicode
type is a String
subclass
that assumes input and output as Python unicode
data,
and in that regard is equivalent to the usage of the
convert_unicode
flag with the String
type.
However, unlike plain String
, it also implies an
underlying column type that is explicitly supporting of non-ASCII
data, such as NVARCHAR
on Oracle and SQL Server.
This can impact the output of CREATE TABLE
statements
and CAST
functions at the dialect level, and can
also affect the handling of bound parameters in some
specific DBAPI scenarios.
The encoding used by the Unicode
type is usually
determined by the DBAPI itself; most modern DBAPIs
feature support for Python unicode
objects as bound
values and result set values, and the encoding should
be configured as detailed in the notes for the target
DBAPI in the Dialects section.
For those DBAPIs which do not support, or are not configured
to accommodate Python unicode
objects
directly, SQLAlchemy does the encoding and decoding
outside of the DBAPI. The encoding in this scenario
is determined by the encoding
flag passed to
create_engine()
.
When using the Unicode
type, it is only appropriate
to pass Python unicode
objects, and not plain str
.
If a plain str
is passed under Python 2, a warning
is emitted. If you notice your application emitting these warnings but
you’re not sure of the source of them, the Python
warnings
filter, documented at
http://docs.python.org/library/warnings.html,
can be used to turn these warnings into exceptions
which will illustrate a stack trace:
import warnings
warnings.simplefilter('error')
For an application that wishes to pass plain bytestrings
and Python unicode
objects to the Unicode
type
equally, the bytestrings must first be decoded into
unicode. The recipe at Coercing Encoded Strings to Unicode illustrates
how this is done.
See also:
UnicodeText
- unlengthed textual counterpart toUnicode
.
sqlalchemy.types.
UnicodeText
(length=None, **kwargs)¶Bases: sqlalchemy.types.Text
An unbounded-length Unicode string type.
See Unicode
for details on the unicode
behavior of this object.
Like Unicode
, usage the UnicodeText
type implies a
unicode-capable type being used on the backend, such as
NCLOB
, NTEXT
.
The SQL standard types always create database column types of the same
name when CREATE TABLE
is issued. Some types may not be supported
on all databases.
sqlalchemy.types.
BIGINT
¶Bases: sqlalchemy.types.BigInteger
The SQL BIGINT type.
sqlalchemy.types.
BINARY
(length=None)¶Bases: sqlalchemy.types._Binary
The SQL BINARY type.
sqlalchemy.types.
BLOB
(length=None)¶Bases: sqlalchemy.types.LargeBinary
The SQL BLOB type.
sqlalchemy.types.
BOOLEAN
(create_constraint=True, name=None, _create_events=True)¶Bases: sqlalchemy.types.Boolean
The SQL BOOLEAN type.
sqlalchemy.types.
CHAR
(length=None, collation=None, convert_unicode=False, unicode_error=None, _warn_on_bytestring=False)¶Bases: sqlalchemy.types.String
The SQL CHAR type.
sqlalchemy.types.
CLOB
(length=None, collation=None, convert_unicode=False, unicode_error=None, _warn_on_bytestring=False)¶Bases: sqlalchemy.types.Text
The CLOB type.
This type is found in Oracle and Informix.
sqlalchemy.types.
DATE
¶Bases: sqlalchemy.types.Date
The SQL DATE type.
sqlalchemy.types.
DATETIME
(timezone=False)¶Bases: sqlalchemy.types.DateTime
The SQL DATETIME type.
sqlalchemy.types.
DECIMAL
(precision=None, scale=None, decimal_return_scale=None, asdecimal=True)¶Bases: sqlalchemy.types.Numeric
The SQL DECIMAL type.
sqlalchemy.types.
FLOAT
(precision=None, asdecimal=False, decimal_return_scale=None, **kwargs)¶Bases: sqlalchemy.types.Float
The SQL FLOAT type.
sqlalchemy.types.
INTEGER
¶Bases: sqlalchemy.types.Integer
The SQL INT or INTEGER type.
sqlalchemy.types.
NCHAR
(length=None, **kwargs)¶Bases: sqlalchemy.types.Unicode
The SQL NCHAR type.
sqlalchemy.types.
NVARCHAR
(length=None, **kwargs)¶Bases: sqlalchemy.types.Unicode
The SQL NVARCHAR type.
sqlalchemy.types.
NUMERIC
(precision=None, scale=None, decimal_return_scale=None, asdecimal=True)¶Bases: sqlalchemy.types.Numeric
The SQL NUMERIC type.
sqlalchemy.types.
REAL
(precision=None, asdecimal=False, decimal_return_scale=None, **kwargs)¶Bases: sqlalchemy.types.Float
The SQL REAL type.
sqlalchemy.types.
SMALLINT
¶Bases: sqlalchemy.types.SmallInteger
The SQL SMALLINT type.
sqlalchemy.types.
TEXT
(length=None, collation=None, convert_unicode=False, unicode_error=None, _warn_on_bytestring=False)¶Bases: sqlalchemy.types.Text
The SQL TEXT type.
sqlalchemy.types.
TIME
(timezone=False)¶Bases: sqlalchemy.types.Time
The SQL TIME type.
sqlalchemy.types.
TIMESTAMP
(timezone=False)¶Bases: sqlalchemy.types.DateTime
The SQL TIMESTAMP type.
sqlalchemy.types.
VARBINARY
(length=None)¶Bases: sqlalchemy.types._Binary
The SQL VARBINARY type.
sqlalchemy.types.
VARCHAR
(length=None, collation=None, convert_unicode=False, unicode_error=None, _warn_on_bytestring=False)¶Bases: sqlalchemy.types.String
The SQL VARCHAR type.
Database-specific types are also available for import from each database’s dialect module. See the Dialects reference for the database you’re interested in.
For example, MySQL has a BIGINT
type and PostgreSQL has an
INET
type. To use these, import them from the module explicitly:
from sqlalchemy.dialects import mysql
table = Table('foo', metadata,
Column('id', mysql.BIGINT),
Column('enumerates', mysql.ENUM('a', 'b', 'c'))
)
Or some PostgreSQL types:
from sqlalchemy.dialects import postgresql
table = Table('foo', metadata,
Column('ipaddress', postgresql.INET),
Column('elements', postgresql.ARRAY(String))
)
Each dialect provides the full set of typenames supported by that backend within its __all__ collection, so that a simple import * or similar will import all supported types as implemented for that backend:
from sqlalchemy.dialects.postgresql import *
t = Table('mytable', metadata,
Column('id', INTEGER, primary_key=True),
Column('name', VARCHAR(300)),
Column('inetaddr', INET)
)
Where above, the INTEGER and VARCHAR types are ultimately from sqlalchemy.types, and INET is specific to the Postgresql dialect.
Some dialect level types have the same name as the SQL standard type, but also provide additional arguments. For example, MySQL implements the full range of character and string types including additional arguments such as collation and charset:
from sqlalchemy.dialects.mysql import VARCHAR, TEXT
table = Table('foo', meta,
Column('col1', VARCHAR(200, collation='binary')),
Column('col2', TEXT(charset='latin1'))
)