Chapter 4. SQLAlchemy Type Engines
This chapter introduces the SQLAlchemy type system. It covers the built-in types provided by SQLAlchemy: database-independent types and database-specific types. It then tells you how to create your own custom types for use in mapping application data onto your database schema.
Type System Overview
When defining the MetaData used by your application, it is necessary to supply the SQL data
type used by each column of each table (unless the tables are defined with
autoload=True, in which case SQLAlchemy provides
the data types for you). These SQL data types are actually instances of
SQLAlchemy-provided classes known as TypeEngines.
TypeEngine objects convert Python values to native
database values and vice versa. For instance, String(40) is an instance of a
TypeEngine that represents a VARCHAR(40).
TypeEngines also supply SQL text for use when
creating tables using metadata.create_all() or
table.create().
SQLAlchemy provides three different ways of constructing types for
use in your application. First, it provides a set of generic
TypeEngines, which are fairly portable across
different database engines. Second, it provides database server-specific
TypeEngines, which can be used to exploit
particular types supported by certain databases. Third, SQLAlchemy allows
you to define application-specific custom
TypeEngines if you wish to further customize object
conversion to/from the database.
Built-in Types
SQLAlchemy provides a fairly complete set of built-in ...