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()
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.
Initialize a new Inspector.
Parameter: | bind – a Connectable, which is typically an instance of Engine or Connection. |
---|
For a dialect-specific instance of Inspector, see Inspector.from_engine()
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.
Construct a new dialect-specific Inspector object from the given engine or connection.
Parameter: | bind – a Connectable, which is typically an instance of Engine or Connection. |
---|
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.
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:
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:
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:
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:
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.
Return all table names in schema.
Parameters: |
|
---|
This should probably not return view names or maybe it should return them with an indicator t or v.
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.
Return definition for view_name.
Parameter: | schema – Optional, retrieve names from a non-default schema. |
---|
Return all view names in schema.
Parameter: | schema – Optional, retrieve names from a non-default schema. |
---|
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: |
|
---|