SQLAlchemy 0.6.2 Documentation

Version: 0.6.2 Last Updated: 07/06/2010 17:51:07
API Reference | Index

Schema Introspection

SQLAlchemy provides rich schema introspection capabilities. The most common methods for this include the “autoload” argument of Table:

from sqlalchemy import create_engine, MetaData, Table
engine = create_engine('...')
meta = MetaData()
user_table = Table('user', meta, autoload=True, autoload_with=engine)

As well as the reflect() method of MetaData:

from sqlalchemy import create_engine, MetaData, Table
engine = create_engine('...')
meta = MetaData()
meta.reflect(engine)
user_table = meta.tables['user']

Further examples of reflection using Table and MetaData can be found at Reflecting Tables.

There is also a low-level inspection interface available for more specific operations, known as the Inspector:

from sqlalchemy import create_engine
from sqlalchemy.engine import reflection
engine = create_engine('...')
insp = reflection.Inspector.from_engine(engine)
print insp.get_table_names()
class sqlalchemy.engine.reflection.Inspector(bind)

Bases: object

Performs database schema inspection.

The Inspector acts as a proxy to the reflection methods of the Dialect, providing a consistent interface as well as caching support for previously fetched metadata.

The preferred method to construct an Inspector is via the Inspector.from_engine() method. I.e.:

engine = create_engine('...')
insp = Inspector.from_engine(engine)

Where above, the Dialect may opt to return an Inspector subclass that provides additional methods specific to the dialect’s target database.

__init__(bind)

Initialize a new Inspector.

Parameters:

For a dialect-specific instance of Inspector, see Inspector.from_engine()

default_schema_name

Return the default schema name presented by the dialect for the current engine’s database user.

E.g. this is typically public for Postgresql and dbo for SQL Server.

classmethod from_engine(bind)

Construct a new dialect-specific Inspector object from the given engine or connection.

Parameters:

This method differs from direct a direct constructor call of Inspector in that the Dialect is given a chance to provide a dialect-specific Inspector instance, which may provide additional methods.

See the example at Inspector.

get_columns(table_name, schema=None, **kw)

Return information about columns in table_name.

Given a string table_name and an optional string schema, return column information as a list of dicts with these keys:

name
the column’s name
type
TypeEngine
nullable
boolean
default
the column’s default value
attrs
dict containing optional column attributes
get_foreign_keys(table_name, schema=None, **kw)

Return information about foreign_keys in table_name.

Given a string table_name, and an optional string schema, return foreign key information as a list of dicts with these keys:

constrained_columns
a list of column names that make up the foreign key
referred_schema
the name of the referred schema
referred_table
the name of the referred table
referred_columns
a list of column names in the referred table that correspond to constrained_columns
name
optional name of the foreign key constraint.
**kw
other options passed to the dialect’s get_foreign_keys() method.
get_indexes(table_name, schema=None, **kw)

Return information about indexes in table_name.

Given a string table_name and an optional string schema, return index information as a list of dicts with these keys:

name
the index’s name
column_names
list of column names in order
unique
boolean
**kw
other options passed to the dialect’s get_indexes() method.
get_pk_constraint(table_name, schema=None, **kw)

Return information about primary key constraint on table_name.

Given a string table_name, and an optional string schema, return primary key information as a dictionary with these keys:

constrained_columns
a list of column names that make up the primary key
name
optional name of the primary key constraint.
get_primary_keys(table_name, schema=None, **kw)

Return information about primary keys in table_name.

Given a string table_name, and an optional string schema, return primary key information as a list of column names.

get_schema_names()

Return all schema names.

get_table_names(schema=None, order_by=None)

Return all table names in schema.

Parameters:
  • schema – Optional, retrieve names from a non-default schema.
  • order_by – Optional, may be the string “foreign_key” to sort the result on foreign key dependencies.

This should probably not return view names or maybe it should return them with an indicator t or v.

get_table_options(table_name, schema=None, **kw)

Return a dictionary of options specified when the table of the given name was created.

This currently includes some options that apply to MySQL tables.

get_view_definition(view_name, schema=None)

Return definition for view_name.

Parameters:
  • schema – Optional, retrieve names from a non-default schema.
get_view_names(schema=None)

Return all view names in schema.

Parameters:
  • schema – Optional, retrieve names from a non-default schema.
reflecttable(table, include_columns)

Given a Table object, load its internal constructs based on introspection.

This is the underlying method used by most dialects to produce table reflection. Direct usage is like:

from sqlalchemy import create_engine, MetaData, Table
from sqlalchemy.engine import reflection

engine = create_engine('...')
meta = MetaData()
user_table = Table('user', meta)
insp = Inspector.from_engine(engine)
insp.reflecttable(user_table, None)
Parameters:
  • table – a Table instance.
  • include_columns – a list of string column names to include in the reflection process. If None, all columns are reflected.
Previous: Database Schema Next: Column and Data Types